<!DOCTYPE html>
<!-- saved from url=(0039)http://www.expressjs.com.cn/4x/api.html -->
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Express 4.x - API 中文手册</title>
      
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      
      <link rel="stylesheet" href="./Express 4.x - API 中文手册_files/style.css">
      <link rel="stylesheet" href="./Express 4.x - API 中文手册_files/dropit.css">
      <link rel="stylesheet" href="./Express 4.x - API 中文手册_files/prism.css">
      <link href="./Express 4.x - API 中文手册_files/font-awesome.min.css" rel="stylesheet">
      <link rel="stylesheet" href="./Express 4.x - API 中文手册_files/css">
  
      <script src="./Express 4.x - API 中文手册_files/hm.js"></script><script data-cfasync="false" src="./Express 4.x - API 中文手册_files/jquery.min.js"></script>
      <script data-cfasync="false" src="./Express 4.x - API 中文手册_files/app.js"></script>
      <script data-cfasync="false" src="./Express 4.x - API 中文手册_files/retina.js"></script>
      <script data-cfasync="false" src="./Express 4.x - API 中文手册_files/dropit.js"></script>
      <script data-cfasync="false" src="./Express 4.x - API 中文手册_files/prism.js"></script>
      <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "//hm.baidu.com/hm.js?1e9527683dc35951cb595bc55ece8e84";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();
      </script></head>

  <body class="">

    <section class="content">

      <header>
        <div id="mobile-menu">
            <div id="nav-button" class="fa fa-bars fa-2x button"></div>
        </div>
        <section id="logo"><a href="http://www.expressjs.com.cn/" class="express">Express</a>
        </section>
        <div id="navbar">
            <ul id="navmenu">
                <li><a href="http://www.expressjs.com.cn/" id="home-menu">首页</a></li>
                <li>
                    <ul id="getting-started-menu" class="menu dropit">
                        <li class="dropit-trigger"><a href="http://www.expressjs.com.cn/starter/installing.html">入门</a>
                            <ul class="dropit-submenu" style="display: none;">
                                <li>
                                  <a href="http://www.expressjs.com.cn/starter/installing.html">
                                    安装
                                  </a>
                                </li>
                                <li>
                                  <a href="http://www.expressjs.com.cn/starter/hello-world.html">
                                    Hello world
                                  </a>
                                </li>
                                <li>
                                <a href="http://www.expressjs.com.cn/starter/generator.html">
                                  Express 应用生成器
                                </a>
                                </li>
                                <li>
                                  <a href="http://www.expressjs.com.cn/starter/basic-routing.html">
                                    基本路由
                                  </a>
                                </li>
                                <li>
                                  <a href="http://www.expressjs.com.cn/starter/static-files.html">
                                    静态文件
                                  </a>
                                </li>
                                <li>
                                  <a href="http://www.expressjs.com.cn/starter/faq.html">
                                    常见问题
                                  </a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li>
                    <ul id="guide-menu" class="menu dropit">
                        <li class="dropit-trigger"><a href="http://www.expressjs.com.cn/guide/routing.html">使用指南</a>
                            <ul class="dropit-submenu" style="display: none;">
                                <li><a href="http://www.expressjs.com.cn/guide/routing.html">路由</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/guide/using-middleware.html">中间件</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/guide/using-template-engines.html">模板引擎</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/guide/error-handling.html">错误处理</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/guide/debugging.html">调试</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/guide/behind-proxies.html">为 Express 设置代理</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/guide/migrating-4.html">升级到 Express 4</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/guide/database-integration.html">数据库集成</a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li>
                    <ul id="application-menu" class="menu dropit">
                        <li class="dropit-trigger"><a href="http://www.expressjs.com.cn/4x/api.html">API 中文手册</a>
                            <ul class="dropit-submenu" style="display: none;">
                                <li><a href="http://www.expressjs.com.cn/4x/api.html">4.x 版本</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/3x/api.html">3.x 版本</a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li>
                    <ul id="advanced-topics-menu" class="menu dropit">
                        <li class="dropit-trigger"><a href="http://www.expressjs.com.cn/advanced/developing-template-engines.html">进阶话题</a>
                            <ul class="dropit-submenu" style="display: none;">
                                <li><a href="http://www.expressjs.com.cn/advanced/developing-template-engines.html">模板引擎</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/advanced/pm.html">进程管理器</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/advanced/security-updates.html">安全更新</a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li>
                    <ul id="resources-menu" class="menu dropit">
                        <li class="dropit-trigger"><a href="http://www.expressjs.com.cn/resources/glossary.html">有用的资源</a>
                            <ul class="dropit-submenu" style="display: none;">
                                <li><a href="http://www.expressjs.com.cn/resources/glossary.html">术语表</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/resources/middleware.html">中间件</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/resources/community.html">社区</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/resources/books-blogs.html">书籍与博客</a>
                                </li>
                                <li><a href="http://www.expressjs.com.cn/resources/applications.html">实例展示</a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
      </header>
      <ul id="menu" style="height: 473px;">
      
          <li id="express-api"><a href="http://www.expressjs.com.cn/4x/api.html#express" class="active">express()</a>
          <ul id="express-menu" class="active">
              <li><em>Methods</em></li>
              <li id="express-static-middleware"><a href="http://www.expressjs.com.cn/4x/api.html#express.static" class="">express.static()</a></li>
          </ul>
          </li>
      
          <li id="app-api"><a href="http://www.expressjs.com.cn/4x/api.html#app" class="">Application</a>
              <ul id="app-menu" class="">
                  <li><em>Properties</em>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.locals" class="">app.locals</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.mountpath" class="">app.mountpath</a>
                  </li>
                  <li><em>Events</em>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.onmount" class="">mount</a>
                  </li>
                  <li><em>Methods</em>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.all" class="">app.all()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.delete.method" class="">app.delete()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.disable" class="">app.disable()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.disabled" class="">app.disabled()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.enable" class="">app.enable()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.enabled" class="">app.enabled()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.engine" class="">app.engine()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.get" class="">app.get()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.get.method" class="">app.get()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.listen" class="">app.listen()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.METHOD" class="">app.METHOD()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.param" class="">app.param()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.path" class="">app.path()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.post.method" class="">app.post()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.put.method" class="">app.put()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.render" class="">app.render()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.route" class="">app.route()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.set" class="">app.set()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#app.use" class="">app.use()</a>
                  </li>
              </ul>
          </li>
          <li id="req-api"><a href="http://www.expressjs.com.cn/4x/api.html#req" class="">Request</a>
              <ul id="req-menu" class="">
                  <li><em>Properties</em>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.app" class="">req.app</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.baseUrl" class="">req.baseUrl</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.body" class="">req.body</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.cookies" class="">req.cookies</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.fresh" class="">req.fresh</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.hostname" class="">req.hostname</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.ip" class="">req.ip</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.ips" class="">req.ips</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.originalUrl" class="">req.originalUrl</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.params" class="">req.params</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.path" class="">req.path</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.protocol" class="">req.protocol</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.query" class="">req.query</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.route" class="">req.route</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.secure" class="">req.secure</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.signedCookies" class="">req.signedCookies</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.stale" class="">req.stale</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.subdomains" class="">req.subdomains</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.xhr" class="">req.xhr</a>
                  </li>
                  <li><em>Methods</em>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.accepts" class="">req.accepts()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.acceptsCharsets" class="">req.acceptsCharsets()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.acceptsEncodings" class="">req.acceptsEncodings()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.acceptsLanguages" class="">req.acceptsLanguages()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.get" class="">req.get()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.is" class="">req.is()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#req.param" class="">req.param()</a>
                  </li>
              </ul>
          </li>
          <li id="res-api"><a href="http://www.expressjs.com.cn/4x/api.html#res" class="">Response</a>
              <ul id="res-menu" class="">
                  <li><em>Properties</em>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.app" class="">res.app      </a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.headersSent" class="">res.headersSent</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.locals" class="">res.locals</a>
                  </li>
                  <li><em>Methods</em>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.append" class="">res.append()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.attachment" class="">res.attachment()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.cookie" class="">res.cookie()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.clearCookie" class="">res.clearCookie()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.download" class="">res.download()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.end" class="">res.end()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.format" class="">res.format()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.get" class="">res.get()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.json" class="">res.json()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.jsonp">res.jsonp()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.links">res.links()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.location">res.location()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.redirect" class="">res.redirect()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.render" class="">res.render()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.send" class="">res.send()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.sendFile" class="">res.sendFile()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.sendStatus">res.sendStatus()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.set">res.set()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.status">res.status()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.type">res.type()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#res.vary">res.vary()</a>
                  </li>
              </ul>
          </li>
          <li id="router-api"><a href="http://www.expressjs.com.cn/4x/api.html#router" class="">Router</a>
              <ul id="router-menu" class="">
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#router" class="">Router()</a>
                  </li>
                  <li><em>Methods</em>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#router.all" class="">router.all()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#router.METHOD" class="">router.METHOD()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#router.param" class="">router.param()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#router.route" class="">router.route()</a>
                  </li>
                  <li><a href="http://www.expressjs.com.cn/4x/api.html#router.use" class="">router.use()</a>
                  </li>
              </ul>
          </li>
      </ul>

      <div id="overlay"></div>
    
      <div id="api-doc">

  <h1>Express 4.x API 中文手册</h1>

  <p><a id="express" class="h2"></a></p>
  <h2>express()</h2>

  <p>创建一个 Express 应用。<code>express()</code> 是一个由 <em>express</em> 模块导出的入口（top-level）函数。</p>

  <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> express <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  <h3 id="express.methods">内置方法</h3>

  <section>
    <h3 id="express.static" class="h2">express.static(root, [options])</h3>

    <p><code>express.static</code> 是 Express 内置的唯一一个中间件。是基于 <a href="https://github.com/expressjs/serve-static">serve-static</a> 开发的，负责托管 Express 应用内的静态资源。</p>

    <p><code>root</code> 参数指的是静态资源文件所在的根目录。</p>

    <p><code>options</code> 对象是可选的，支持以下属性：</p>

    <table>
      <thead>
        <tr>
          <th>属性</th>
          <th>描述</th>
          <th>类型</th>
          <th>默认值</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><code>dotfiles</code></td>
          <td>Option for serving dotfiles. Possible values are “allow”, “deny”, and “ignore”</td>
          <td>String</td>
          <td>“ignore”</td>
        </tr>
        <tr>
          <td><code>etag</code></td>
          <td>Enable or disable etag generation</td>
          <td>Boolean</td>
          <td><code>true</code></td>
        </tr>
        <tr>
          <td><code>extensions</code></td>
          <td>Sets file extension fallbacks.</td>
          <td>Boolean</td>
          <td><code>false</code></td>
        </tr>
        <tr>
          <td><code>index</code></td>
          <td>Sends directory index file. Set <code>false</code> to disable directory indexing.</td>
          <td>Mixed</td>
          <td>“index.html”</td>
        </tr>
        <tr>
          <td><code>lastModified</code></td>
          <td>Set the <code>Last-Modified</code> header to the last modified date of the file on the OS. Possible values are <code>true</code> or <code>false</code>.</td>
          <td>Boolean</td>
          <td><code>true</code></td>
        </tr>
        <tr>
          <td><code>maxAge</code></td>
          <td>Set the max-age property of the Cache-Control header in milliseconds or a string in <a href="https://www.npmjs.org/package/ms">ms format</a></td>
          <td>Number</td>
          <td>0</td>
        </tr>
        <tr>
          <td><code>redirect</code></td>
          <td>Redirect to trailing “/” when the pathname is a directory.</td>
          <td>Boolean</td>
          <td><code>true</code></td>
        </tr>
        <tr>
          <td><code>setHeaders</code></td>
          <td>Function for setting HTTP headers to serve with the file.</td>
          <td>Function</td>
          <td>&nbsp;</td>
        </tr>
      </tbody>
    </table>

    <p>关于此中间件的细节，请参考 <a href="http://www.expressjs.com.cn/starter/static-files.html">通过 Express 托管静态资源文件</a>。</p>

  </section>

  <p><a id="app" class="h2"></a></p>
  <h2>Application</h2>

  <p>The <code>app</code> object conventionally denotes the Express application.
Create it by calling the top-level <code>express()</code> function exported by the Express module:</p>

  <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> express <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'hello world'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">listen<span class="token punctuation">(</span></span><span class="token number">3000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  <p>The <code>app</code> object has methods for</p>

  <ul>
    <li>Routing HTTP requests; see for example, <a href="http://www.expressjs.com.cn/4x/api.html#app.METHOD" class="active">app.METHOD</a> and <a href="http://www.expressjs.com.cn/4x/api.html#app.param" class="active">app.param</a>.</li>
    <li>Configuring middleware; see <a href="http://www.expressjs.com.cn/4x/api.html#app.route" class="active">app.route</a>.</li>
    <li>Rendering HTML views; see <a href="http://www.expressjs.com.cn/4x/api.html#app.render" class="active">app.render</a>.</li>
    <li>Registering a template engine; see <a href="http://www.expressjs.com.cn/4x/api.html#app.engine" class="active">app.engine</a>.</li>
  </ul>

  <p>It also has settings (properties) that affect how the application behaves;
for more information, see <a href="http://www.expressjs.com.cn/4x/api.html#app.settings.table">Application settings</a>.</p>

  <h3 id="app.properties">Properties</h3>

  <section>
    <h3 id="app.locals">app.locals</h3>

    <p>The <code>app.locals</code> object is a JavaScript object, and its 
properties are local variables within the application.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span>locals<span class="token punctuation">.</span>title
<span class="token comment" spellcheck="true">// =&gt; 'My App'
</span>
app<span class="token punctuation">.</span>locals<span class="token punctuation">.</span>email
<span class="token comment" spellcheck="true">// =&gt; 'me@myapp.com'
</span></code></pre>

    <p>Once set, the value of <code>app.locals</code> properties persist throughout the life of the application,
in contrast with <a href="http://www.expressjs.com.cn/4x/api.html#res.locals" class="active">res.locals</a> properties that
are valid only for the lifetime of the request.</p>

    <p>You can access local variables in templates rendered within the application.
This is useful for providing helper functions to templates, as well as app-level data.
Note, however, that you cannot access local variables in middleware.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span>locals<span class="token punctuation">.</span>title <span class="token operator">=</span> <span class="token string">'My App'</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span>locals<span class="token punctuation">.</span>strftime <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'strftime'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span>locals<span class="token punctuation">.</span>email <span class="token operator">=</span> <span class="token string">'me@myapp.com'</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="app.mountpath">app.mountpath</h3>

    <p>The <code>app.mountpath</code> property is the path pattern(s) on which a sub app was mounted.</p>

    <div class="doc-box doc-info">
      <p>A sub app is an instance of <code>express</code> which may be used for handling the request to a route.</p>
    </div>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> express <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// the main app
</span><span class="token keyword">var</span> admin <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// the sub app
</span>
admin<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>admin<span class="token punctuation">.</span>mountpath<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// /admin
</span>  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Admin Homepage'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/admin'</span><span class="token punctuation">,</span> admin<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// mount the sub app
</span></code></pre>

    <p>It is similar to the <a href="http://www.expressjs.com.cn/4x/api.html#req.baseUrl" class="active">baseUrl</a> property of the <code>req</code> object, except <code>req.baseUrl</code> returns the matched URL path, instead of the matched pattern(s).</p>

    <p>If a sub-app is mounted on multiple path patterns, <code>app.mountpath</code> returns the list of patterns it is mounted on, as shown in the following example.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> admin <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

admin<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>admin<span class="token punctuation">.</span>mountpath<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// [ '/adm*n', '/manager' ]
</span>  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Admin Homepage'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token keyword">var</span> secret <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
secret<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>secret<span class="token punctuation">.</span>mountpath<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// /secr*t
</span>  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Admin Secret'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

admin<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/secr*t'</span><span class="token punctuation">,</span> secret<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// load the 'secret' router on '/secr*t', on the 'admin' sub app
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'/adm*n'</span><span class="token punctuation">,</span> <span class="token string">'/manager'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> admin<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// load the 'admin' router on '/adm*n' and '/manager', on the parent app
</span></code></pre>

  </section>

  <h3 id="app.events">Events</h3>

  <section>
    <h3 id="app.onmount">app.on('mount', callback(parent))</h3>

    <p>The <code>mount</code> event is fired on a sub-app, when it is mounted on a parent app. The parent app is passed to the callback function.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> admin <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

admin<span class="token punctuation">.</span><span class="token function">on<span class="token punctuation">(</span></span><span class="token string">'mount'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>parent<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'Admin Mounted'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>parent<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// refers to the parent app
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

admin<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Admin Homepage'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/admin'</span><span class="token punctuation">,</span> admin<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <h3 id="app.methods">Methods</h3>

  <section>
    <h3 id="app.all">app.all(path, callback [, callback ...])</h3>

    <p>This method is like the standard <a href="http://www.expressjs.com.cn/4x/api.html#app.METHOD" class="active">app.METHOD()</a> methods,
except it matches all HTTP verbs.</p>

    <p>It’s useful for mapping “global” logic for specific path prefixes or arbitrary matches.
For example, if you put the following at the top of all other
route definitions, it requires that all routes from that point on
require authentication, and automatically load a user. Keep in mind
that these callbacks do not have to act as end-points: <code>loadUser</code>
can perform a task, then call <code>next()</code> to continue matching subsequent
routes.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token string">'*'</span><span class="token punctuation">,</span> requireAuthentication<span class="token punctuation">,</span> loadUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Or the equivalent:</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token string">'*'</span><span class="token punctuation">,</span> requireAuthentication<span class="token punctuation">)</span>
app<span class="token punctuation">.</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token string">'*'</span><span class="token punctuation">,</span> loadUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Another example is white-listed “global” functionality. 
The example is much like before, however it only restricts paths that start with
“/api”:</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token string">'/api/*'</span><span class="token punctuation">,</span> requireAuthentication<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="app.delete.method">app.delete(path, callback [, callback ...])</h3>

    <p>Routes HTTP DELETE requests to the specified path with the specified callback functions.
For more information, see the <a href="http://www.expressjs.com.cn/guide/routing.html">routing guide</a>.</p>

    <p>You can provide multiple callback functions that behave just like middleware, except
these callbacks can invoke <code>next('route')</code> to bypass the remaining route
callback(s). You can use this mechanism to impose pre-conditions on a route, then pass control
to subsequent routes if there’s no reason to proceed with the current route.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">delete</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'DELETE request to homepage'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="app.disable">app.disable(name)</h3>

    <p>Sets the Boolean setting <code>name</code> to <code>false</code>, where <code>name</code> is one of the properties from the <a href="http://www.expressjs.com.cn/4x/api.html#app.settings.table">app settings table</a>.
Calling <code>app.set('foo', false)</code> for a Boolean property is the same as calling <code>app.disable('foo')</code>.</p>

    <p>For example:</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">disable<span class="token punctuation">(</span></span><span class="token string">'trust proxy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'trust proxy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; false
</span></code></pre>

  </section>

  <section>
    <h3 id="app.disabled">app.disabled(name)</h3>

    <p>Returns <code>true</code> if the Boolean setting <code>name</code> is disabled (<code>false</code>), where <code>name</code> is one of the properties from
the <a href="http://www.expressjs.com.cn/4x/api.html#app.settings.table">app settings table</a>.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">disabled<span class="token punctuation">(</span></span><span class="token string">'trust proxy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; true
</span>
app<span class="token punctuation">.</span><span class="token function">enable<span class="token punctuation">(</span></span><span class="token string">'trust proxy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">disabled<span class="token punctuation">(</span></span><span class="token string">'trust proxy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; false
</span></code></pre>

  </section>

  <section>
    <h3 id="app.enable">app.enable(name)</h3>

    <p>Sets the Boolean setting <code>name</code> to <code>true</code>, where <code>name</code> is one of the properties from the <a href="http://www.expressjs.com.cn/4x/api.html#app.settings.table">app settings table</a>.
Calling <code>app.set('foo', true)</code> for a Boolean property is the same as calling <code>app.enable('foo')</code>.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">enable<span class="token punctuation">(</span></span><span class="token string">'trust proxy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'trust proxy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; true
</span></code></pre>

  </section>

  <section>
    <h3 id="app.enabled">app.enabled(name)</h3>

    <p>Returns <code>true</code> if the setting <code>name</code> is enabled (<code>true</code>), where <code>name</code> is one of the
properties from the <a href="http://www.expressjs.com.cn/4x/api.html#app.settings.table">app settings table</a>.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">enabled<span class="token punctuation">(</span></span><span class="token string">'trust proxy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; false
</span>
app<span class="token punctuation">.</span><span class="token function">enable<span class="token punctuation">(</span></span><span class="token string">'trust proxy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">enabled<span class="token punctuation">(</span></span><span class="token string">'trust proxy'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; true
</span></code></pre>

  </section>

  <section>
    <h3 id="app.engine">app.engine(ext, callback)</h3>

    <p>Registers the given template engine <code>callback</code> as <code>ext</code>.</p>

    <p>By default, Express will <code>require()</code> the engine based on the file extension.
For example, if you try to render a “foo.jade” file, Express invokes the
following internally, and caches the <code>require()</code> on subsequent calls to increase
performance.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">engine<span class="token punctuation">(</span></span><span class="token string">'jade'</span><span class="token punctuation">,</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'jade'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__express<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Use this method for engines that do not provide <code>.__express</code> out of the box,
or if you wish to “map” a different extension to the template engine.</p>

    <p>For example, to map the EJS template engine to “.html” files:</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">engine<span class="token punctuation">(</span></span><span class="token string">'html'</span><span class="token punctuation">,</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'ejs'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>renderFile<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>In this case, EJS provides a <code>.renderFile()</code> method with
the same signature that Express expects: <code>(path, options, callback)</code>,
though note that it aliases this method as <code>ejs.__express</code> internally
so if you’re using “.ejs” extensions you don’t need to do anything.</p>

    <p>Some template engines do not follow this convention.  The
<a href="https://github.com/tj/consolidate.js">consolidate.js</a> library maps Node template engines to follow this convention,
so they work seemlessly with Express.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> engines <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'consolidate'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">engine<span class="token punctuation">(</span></span><span class="token string">'haml'</span><span class="token punctuation">,</span> engines<span class="token punctuation">.</span>haml<span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">engine<span class="token punctuation">(</span></span><span class="token string">'html'</span><span class="token punctuation">,</span> engines<span class="token punctuation">.</span>hogan<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="app.get">app.get(name)</h3>

    <p>Returns the value of <code>name</code> app setting, where <code>name</code> is one of strings in the
<a href="http://www.expressjs.com.cn/4x/api.html#app.settings.table">app settings table</a>. For example:</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'title'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; undefined
</span>
app<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'title'</span><span class="token punctuation">,</span> <span class="token string">'My Site'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'title'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; "My Site"
</span></code></pre>

  </section>

  <section>
    <h3 id="app.get.method">app.get(path, callback [, callback ...])</h3>

    <p>Routes HTTP GET requests to the specified path with the specified callback functions.
For more information, see the <a href="http://www.expressjs.com.cn/guide/routing.html">routing guide</a>.</p>

    <p>You can provide multiple callback functions that behave just like middleware, except
these callbacks can invoke <code>next('route')</code> to bypass the remaining route callback(s).
You can use this mechanism to impose pre-conditions on a route, then pass control to
subsequent routes if there’s no reason to proceed with the current route.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'GET request to homepage'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="app.listen">app.listen(port, [hostname], [backlog], [callback])</h3>

    <p>Binds and listens for connections on the specified host and port.
This method is identical to Node’s <a href="http://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback">http.Server.listen()</a>.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> express <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">listen<span class="token punctuation">(</span></span><span class="token number">3000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>The <code>app</code> returned by <code>express()</code> is in fact a JavaScript
<code>Function</code>, designed to be passed to Node’s HTTP servers as a callback
to handle requests. This makes it easy to provide both HTTP and HTTPS versions of
your app with the same code base, as the app does not inherit from these
(it is simply a callback):</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> express <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> https <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'https'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> http <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'http'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

http<span class="token punctuation">.</span><span class="token function">createServer<span class="token punctuation">(</span></span>app<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">listen<span class="token punctuation">(</span></span><span class="token number">80</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
https<span class="token punctuation">.</span><span class="token function">createServer<span class="token punctuation">(</span></span>options<span class="token punctuation">,</span> app<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">listen<span class="token punctuation">(</span></span><span class="token number">443</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>The <code>app.listen()</code> method is a convenience method for the following (for HTTP only):</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span>listen <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> server <span class="token operator">=</span> http<span class="token punctuation">.</span><span class="token function">createServer<span class="token punctuation">(</span></span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> server<span class="token punctuation">.</span>listen<span class="token punctuation">.</span><span class="token function">apply<span class="token punctuation">(</span></span>server<span class="token punctuation">,</span> arguments<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="app.METHOD">app.METHOD(path, callback [, callback ...])</h3>

    <p>Routes an HTTP request, where METHOD is the HTTP method of the request, such as GET,
PUT, POST, and so on, in lowercase. Thus, the actual methods are <code>app.get()</code>,
<code>app.post()</code>, <code>app.put()</code>, and so on.  See below for the complete list.</p>

    <p>For more information, see the <a href="http://www.expressjs.com.cn/guide/routing.html">routing guide</a>.</p>

    <p>Express supports the following routing methods corresponding to the HTTP methods of the same names:</p>

    <table style="border: 0px; background: none">
<tbody><tr>
<td style="background: none; border: 0px;">
          <ul>
            <li><code>checkout</code></li>
            <li><code>connect</code></li>
            <li><code>copy</code></li>
            <li><code>delete</code></li>
            <li><code>get</code></li>
            <li><code>head</code></li>
            <li><code>lock</code></li>
            <li><code>merge</code></li>
            <li><code>mkactivity</code></li>
          </ul>
        </td>
<td style="background: none; border: 0px;">
          <ul>
            <li><code>mkcol</code></li>
            <li><code>move</code></li>
            <li><code>m-search</code></li>
            <li><code>notify</code></li>
            <li><code>options</code></li>
            <li><code>patch</code></li>
            <li><code>post</code></li>
            <li><code>propfind</code></li>
            <li><code>proppatch</code></li>
          </ul>
        </td>
<td style="background: none; border: 0px;">
          <ul>
            <li><code>purge</code></li>
            <li><code>put</code></li>
            <li><code>report</code></li>
            <li><code>search</code></li>
            <li><code>subscribe</code></li>
            <li><code>trace</code></li>
            <li><code>unlock</code></li>
            <li><code>unsubscribe</code></li>
          </ul>
        </td>
</tr>
</tbody></table>

    <div class="doc-box doc-info">
      <p>To route methods which translate to invalid JavaScript variable names, use the bracket notation. For example, 
  <code>app['m-search']('/', function ...</code>.</p>
    </div>

    <p>You can provide multiple callback functions that behave just like middleware, except
that these callbacks can invoke <code>next('route')</code> to bypass
the remaining route callback(s). You can use this mechanism to impose pre-conditions
on a route, then pass control to subsequent routes if there is no reason to proceed with the current route.</p>

    <div class="doc-box doc-info">
      <p>The API documentation has explicit entries only for the most popular HTTP methods <code>app.get()</code>,
  <code>app.post()</code>, <code>app.put()</code>, and <code>app.delete()</code>.
  However, the other methods listed above work in exactly the same way.</p>
    </div>

    <p>There is a special routing method, <code>app.all()</code>, that is not derived from any HTTP method.
It loads middleware at a path for all request methods.</p>

    <p>In the following example, the handler is executed for requests to “/secret” whether using
GET, POST, PUT, DELETE, or any other HTTP request method.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token string">'/secret'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'Accessing the secret section ...'</span><span class="token punctuation">)</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// pass control to the next handler
</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

  </section>

  <section>
    <h3 id="app.param">app.param([name], callback)</h3>

    <p>Add callback triggers to route parameters, where <code>name</code> is the name of the parameter or an array of them, and <code>function</code> is the callback function. The parameters of the callback function are the request object, the response object, the next middleware, and the value of the parameter, in that order.</p>

    <p>If <code>name</code> is an array, the <code>callback</code> trigger is registered for each parameter declared in it, in the order in which they are declared. Furthermore, for each declared parameter except the last one, a call to <code>next</code> inside the callback will call the callback for the next declared parameter. For the last parameter, a call to <code>next</code> will call the next middleware in place for the route currently being processed, just like it would if <code>name</code> were just a string.</p>

    <p>For example, when <code>:user</code> is present in a route path, you may map user loading logic to automatically provide <code>req.user</code> to the route, or perform validations on the parameter input.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'user'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">,</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token comment" spellcheck="true">// try to get the user details from the User model and attach it to the request object
</span>  User<span class="token punctuation">.</span><span class="token function">find<span class="token punctuation">(</span></span>id<span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> user<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">next<span class="token punctuation">(</span></span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>user<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      req<span class="token punctuation">.</span>user <span class="token operator">=</span> user<span class="token punctuation">;</span>
      <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token function">next<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">'failed to load user'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Param callback functions are local to the router on which they are defined. They are not inherited by mounted apps or routers. Hence, param callbacks defined on <code>app</code> will be triggered only by route parameters defined on <code>app</code> routes.</p>

    <p>All param callbacks will be called before any handler of any route in which the param occurs, and they will each be called only once in a request-response cycle, even if the parameter is matched in multiple routes, as shown in the following examples.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">,</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'CALLED ONLY ONCE'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'although this matches'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'and this matches too'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  res<span class="token punctuation">.</span><span class="token function">end<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>On <code>GET /user/42</code>, the following is printed:</p>

    <pre><code>CALLED ONLY ONCE
although this matches
and this matches too
</code></pre>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'id'</span><span class="token punctuation">,</span> <span class="token string">'page'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">,</span> value<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'CALLED ONLY ONCE with'</span><span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id/:page'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'although this matches'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id/:page'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'and this matches too'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  res<span class="token punctuation">.</span><span class="token function">end<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>On <code>GET /user/42/3</code>, the following is printed:</p>

    <pre><code>CALLED ONLY ONCE with 42
CALLED ONLY ONCE with 3
although this matches
and this matches too
</code></pre>

    <div class="doc-box doc-warn">
      <p>The following section describes <code>app.param(callback)</code>, which is deprecated as of v4.11.0.</p>
    </div>

    <p>The behavior of the <code>app.param(name, callback)</code> method can be altered entirely by passing only a function to <code>app.param()</code>. This function is a custom implementation of how <code>app.param(name, callback)</code> should behave - it accepts two parameters and must return a middleware.</p>

    <p>The first parameter of this function is the name of the URL parameter that should be captured, the second parameter can be any JavaScript object which might be used for returning the middleware implementation.</p>

    <p>The middleware returned by the function decides the behavior of what happens when a URL parameter is captured.</p>

    <p>In this example, the <code>app.param(name, callback)</code> signature is modified to <code>app.param(name, accessId)</code>. Instead of accepting a name and a callback, <code>app.param()</code> will now accept a name and a number.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> express <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// customizing the behavior of app.param()
</span>app<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>param<span class="token punctuation">,</span> option<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>val <span class="token operator">==</span> option<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>
      res<span class="token punctuation">.</span><span class="token function">sendStatus<span class="token punctuation">(</span></span><span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// using the customized app.param()
</span>app<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">,</span> <span class="token number">1337</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// route to trigger the capture
</span>app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'OK'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span><span class="token function">listen<span class="token punctuation">(</span></span><span class="token number">3000</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'Ready'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

    <p>In this example, the <code>app.param(name, callback)</code> signature remains the same, but instead of a middleware callback, a custom data type checking function has been defined to validate the data type of the user id.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>param<span class="token punctuation">,</span> validator<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">validator<span class="token punctuation">(</span></span>val<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>
      res<span class="token punctuation">.</span><span class="token function">sendStatus<span class="token punctuation">(</span></span><span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>candidate<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token operator">!</span><span class="token function">isNaN<span class="token punctuation">(</span></span><span class="token function">parseFloat<span class="token punctuation">(</span></span>candidate<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">isFinite<span class="token punctuation">(</span></span>candidate<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <div class="doc-box doc-info">
      <p>The ‘<code>.</code>’ character can’t be used to capture a character in your capturing regexp. For example you can’t use <code>'/user-.+/'</code> to capture <code>'users-gami'</code>, use <code>[\\s\\S]</code> or <code>[\\w\\W]</code> instead (as in <code>'/user-[\\s\\S]+/'</code>.</p>

      <p>Examples:</p>

      <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">//captures '1-a_6' but not '543-azser-sder'
</span>router<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/[0-9]+-[[\\w]]*'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

<span class="token comment" spellcheck="true">//captures '1-a_6' and '543-az(ser"-sder' but not '5-a s'
</span>router<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/[0-9]+-[[\\S]]*'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

<span class="token comment" spellcheck="true">//captures all (equivalent to '.*')
</span>router<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'[[\\s\\S]]*'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
</code></pre>

    </div>

  </section>

  <section>
    <h3 id="app.path">app.path()</h3>

    <p>Returns the canonical path of the app, a string.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
  <span class="token punctuation">,</span> blog <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span>
  <span class="token punctuation">,</span> blogAdmin <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/blog'</span><span class="token punctuation">,</span> blog<span class="token punctuation">)</span><span class="token punctuation">;</span>
blog<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/admin'</span><span class="token punctuation">,</span> blogAdmin<span class="token punctuation">)</span><span class="token punctuation">;</span>

console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>app<span class="token punctuation">.</span><span class="token function">path<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// ''
</span>console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>blog<span class="token punctuation">.</span><span class="token function">path<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// '/blog'
</span>console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>blogAdmin<span class="token punctuation">.</span><span class="token function">path<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// '/blog/admin'
</span></code></pre>

    <p>The behavior of this method can become very complicated in complex cases of mounted apps:
it is usually better to use <a href="http://www.expressjs.com.cn/4x/api.html#req.baseUrl" class="active">req.baseUrl</a> to get the canonical path of the app.</p>

  </section>

  <section>
    <h3 id="app.post.method">app.post(path, callback [, callback ...])</h3>

    <p>Routes HTTP POST requests to the specified path with the specified callback functions.
For more information, see the <a href="http://www.expressjs.com.cn/guide/routing.html">routing guide</a>.</p>

    <p>You can provide multiple callback functions that behave just like middleware,
except that these callbacks can invoke <code>next('route')</code> to bypass the
remaining route callback(s). You can use this mechanism to impose pre-conditions on
a route, then pass control to subsequent routes if there’s no reason to proceed with
the current route.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">post<span class="token punctuation">(</span></span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'POST request to homepage'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="app.put.method">app.put(path, callback [, callback ...])</h3>

    <p>Routes HTTP PUT requests to the specified path with the specified callback functions.
For more information, see the <a href="http://www.expressjs.com.cn/guide/routing.html">routing guide</a>.</p>

    <p>You can provide multiple callback functions that behave just like middleware,
except that these callbacks can invoke <code>next('route')</code> to bypass the
remaining route callback(s). You can use this mechanism to impose pre-conditions on
a route, then pass control to subsequent routes if there’s no reason to proceed with
the current route.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">put<span class="token punctuation">(</span></span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'PUT request to homepage'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="app.render">app.render(view, [locals], callback)</h3>

    <p>Returns the rendered HTML of a view via the <code>callback</code> function. It accepts an optional parameter
that is an object containing local variables for the view. It is like <a href="http://www.expressjs.com.cn/4x/api.html#res.render" class="active">res.render()</a>,
except it cannot send the rendered view to the client on its own.</p>

    <div class="doc-box doc-info">
      <p>Think of <code>app.render()</code> as a utility function for generating rendered view strings.
Internally <code>res.render()</code> uses <code>app.render()</code> to render views.</p>
    </div>

    <div class="doc-box doc-notice">
      <p>The local variable <code>cache</code> is reserved for enabling view cache. Set it to <code>true</code>, if you want to
cache view during development; view caching is enabled in production by default.</p>
    </div>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">render<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> html<span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// ...
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">render<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> name<span class="token punctuation">:</span> <span class="token string">'Tobi'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> html<span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// ...
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="app.route">app.route(path)</h3>

    <p>Returns an instance of a single route, which you can then use to handle HTTP verbs with optional middleware.
Use <code>app.route()</code> to avoid duplicate route names (and thus typo errors).</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">route<span class="token punctuation">(</span></span><span class="token string">'/events'</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// runs for all HTTP verbs first
</span>  <span class="token comment" spellcheck="true">// think of it as route specific middleware!
</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">json<span class="token punctuation">(</span></span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">post<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// maybe add a new event...
</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

  </section>

  <section>
    <h3 id="app.set">app.set(name, value)</h3>

    <p>Assigns setting <code>name</code> to <code>value</code>, where <code>name</code> is one of the properties from
the <a href="http://www.expressjs.com.cn/4x/api.html#app.settings.table">app settings table</a>.</p>

    <p>Calling <code>app.set('foo', true)</code> for a Boolean property is the same as calling
<code>app.enable('foo')</code>. Similarly, calling <code>app.set('foo', false)</code> for a Boolean
property is the same as calling <code>app.disable('foo')</code>.</p>

    <p>Retrieve the value of a setting with <a href="http://www.expressjs.com.cn/4x/api.html#app.get" class="active"><code>app.get()</code></a>.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'title'</span><span class="token punctuation">,</span> <span class="token string">'My Site'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'title'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// "My Site"
</span></code></pre>

    <h4 id="app.settings.table">Application Settings</h4>

    <p>If <code>name</code> is one of the application settings, it affects the behavior of the application. The following table lists application settings.</p>

    <div class="table-scroller">
  <table class="doctable" border="1">
    <thead><tr><th id="app-settings-property">Property</th><th>Type</th><th>Value</th><th>Default</th></tr></thead>
    <tbody>
    <tr>
  <td>
              <p><code>case sensitive routing</code></p>
            </td>
      <td>Boolean</td>
      <td>Enable case sensitivity.</td>
      <td>Disabled. Treats "/Foo" and "/foo" as the same.</td>
    </tr>
    <tr>
  <td>
              <p><code>env</code></p>
            </td>
      <td>String</td>
      <td>Environment mode.</td>
  <td>
              <p><code>process.env.NODE_ENV</code> (<code>NODE_ENV</code> environment variable) or “development”.</p>
            </td>
    </tr>
    <tr>
  <td>
              <p><code>etag</code></p>
            </td>
      <td>Varied</td>
  <td>
              <p>Set the ETag response header. For possible values, see the <a href="http://www.expressjs.com.cn/4x/api.html#etag.options.table"><code>etag</code> options table</a>.</p>

              <p><a href="http://en.wikipedia.org/wiki/HTTP_ETag">More about the HTTP ETag header</a>.</p>
            </td>
      <td></td>
    </tr>
    <tr>
  <td>
              <p><code>jsonp callback name</code></p>
            </td>
      <td>String</td>
      <td>Specifies the default JSONP callback name.</td>
  <td>
              <p><code>?callback=</code></p>
            </td>
    </tr>
    <tr>
  <td>
              <p><code>json replacer</code></p>
            </td>
      <td>String</td>
      <td>JSON replacer callback.</td>
  <td>
              <p><code>null</code></p>
            </td>
    </tr>
    <tr>
  <td>
              <p><code>json spaces</code></p>
            </td>
      <td>Number</td>
      <td>When set, sends prettified JSON string indented with the specified amount of spaces.</td>
      <td>Disabled.</td>
    </tr>
    <tr>
  <td>
              <p><code>query parser</code></p>
            </td>
      <td>String</td>
  <td>
              <p>The query parser to use, either “simple” or “extended”. The simple query parser is based on Node’s native query parser, <a href="http://nodejs.org/api/querystring.html">querystring</a>. The extended query parser is based on <a href="https://www.npmjs.org/package/qs">qs</a>.</p>
            </td>
      <td>"extended"</td>
    </tr>
    <tr>
  <td>
              <p><code>strict routing</code></p>
            </td>
      <td>Boolean</td>
      <td>Enable strict routing.</td>
      <td>Disabled. Treats "/foo" and "/foo/" as the same by the router.</td>
    </tr>
    <tr>
  <td>
              <p><code>subdomain offset</code></p>
            </td>
      <td>Number</td>
      <td>The number of dot-separated parts of the host to remove to access subdomain.</td>
      <td>2</td>
    </tr>
    <tr>
  <td>
              <p><code>trust proxy</code></p>
            </td>
      <td>Varied</td>
  <td>
              <p>Indicates the app is behind a front-facing proxy, and to use the <code>X-Forwarded-*</code> headers to determine the connection and the IP address of the client. NOTE: <code>X-Forwarded-*</code> headers are easily spoofed and the detected IP addresses are unreliable.</p>

              <p><code>trust proxy</code> is disabled by default. When enabled, Express attempts to determine the IP address of the client connected through the front-facing proxy, or series of proxies. The <code>req.ips</code> property, then, contains an array of IP addresses the client is connected through. To enable it, use the values described in the <a href="http://www.expressjs.com.cn/4x/api.html#trust.proxy.options.table"><code>trust proxy</code> options table</a>.</p>

              <p>The <code>trust proxy</code> setting is implemented using the <a href="https://www.npmjs.org/package/proxy-addr">proxy-addr</a> package.  For more information, see its documentation.</p>
            </td>
      <td>Disabled.</td>
    </tr>
    <tr>
  <td>
              <p><code>views</code></p>
            </td>
      <td>String or Array</td>
      <td>A directory or an array of directories for the application's views. If an array, the views are looked up in the order they occur in the array.</td>
  <td>
              <p><code>process.cwd() + '/views'</code></p>
            </td>
    </tr>
    <tr>
  <td>
              <p><code>view cache</code></p>
            </td>
      <td>Boolean</td>
      <td>Enables view template compilation caching.</td>
  <td>
              <p><code>true</code> in production.</p>
            </td>
    </tr>
    <tr>
  <td>
              <p><code>view engine</code></p>
            </td>
      <td>String</td>
      <td>The default engine extension to use when omitted.</td>
      <td></td>
    </tr>
    <tr>
  <td>
              <p><code>x-powered-by</code></p>
            </td>
      <td>Boolean</td>
      <td>Enables the "X-Powered-By: Express" HTTP header.</td>
  <td>
              <p><code>true</code></p>
            </td>
    </tr>
    </tbody>
  </table>

  <h5 id="trust.proxy.options.table">Options for `trust proxy` setting</h5>

  <table class="doctable" border="1">
    <thead><tr><th>Type</th><th>Value</th></tr></thead>
    <tbody>
      <tr>
        <td>Boolean</td>
  <td>
              <p>If <code>true</code>, the client’s IP address is understood as the left-most entry in the <code>X-Forwarded-*</code> header.</p>

              <p>If <code>false</code>, the app is understood as directly facing the Internet and the client’s IP address is derived from <code>req.connection.remoteAddress</code>. This is the default setting.</p>
            </td>
      </tr>
      <tr>
        <td>IP addresses</td>
  <td>
              <p>An IP address, subnet, or an array of IP addresses, and subnets to trust. The following is the list of pre-configured subnet names.</p>

              <ul>
                <li>loopback - <code>127.0.0.1/8</code>, <code>::1/128</code></li>
                <li>linklocal - <code>169.254.0.0/16</code>, <code>fe80::/10</code></li>
                <li>uniquelocal - <code>10.0.0.0/8</code>, <code>172.16.0.0/12</code>, <code>192.168.0.0/16</code>, <code>fc00::/7</code></li>
              </ul>

              <p>Set IP addresses in any of the following ways:</p>

              <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'trust proxy'</span><span class="token punctuation">,</span> <span class="token string">'loopback'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// specify a single subnet
</span>  app<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'trust proxy'</span><span class="token punctuation">,</span> <span class="token string">'loopback, 123.123.123.123'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// specify a subnet and an address
</span>  app<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'trust proxy'</span><span class="token punctuation">,</span> <span class="token string">'loopback, linklocal, uniquelocal'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// specify multiple subnets as CSV
</span>  app<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'trust proxy'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'loopback'</span><span class="token punctuation">,</span> <span class="token string">'linklocal'</span><span class="token punctuation">,</span> <span class="token string">'uniquelocal'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// specify multiple subnets as an array</span></code></pre>

              <p>When specified, the IP addresses or the subnets are excluded from the address determination process, and the untrusted IP address nearest to the application server is determined as the client’s IP address.</p>
            </td>
      </tr>
      <tr>
        <td>Number</td>
  <td>
              <p>Trust the <code>n</code>th hop from the front-facing proxy server as the client.</p>
            </td>
      </tr>
      <tr>
        <td>Function</td>
  <td>
              <p>Custom trust implementation. Use this only if you know what you are doing.</p>
              <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'trust proxy'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>ip<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>ip <span class="token operator">===</span> <span class="token string">'127.0.0.1'</span> <span class="token operator">||</span> ip <span class="token operator">===</span> <span class="token string">'123.123.123.123'</span><span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token keyword">true</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// trusted IPs
</span>    <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token keyword">false</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre>
            </td>
      </tr>
    </tbody>
  </table>

  <h5 id="etag.options.table">Options for `etag` setting</h5>

  <table class="doctable" border="1">
    <thead><tr><th>Type</th><th>Value</th></tr></thead>
    <tbody>
      <tr>
        <td>Boolean</td>
  <td>
              <p><code>true</code> enables weak ETag. This is the default setting.<br>
  <code>false</code> disables ETag altogether.</p>
            </td>
      </tr>
      <tr>
        <td>String</td>
        <td>
            If "strong", enables strong ETag.<br>
            If "weak", enables weak ETag.
        </td>
      </tr>
      <tr>
        <td>Function</td>
  <td>
              <p>Custom ETag function implementation. Use this only if you know what you are doing.</p>

              <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'etag'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>body<span class="token punctuation">,</span> encoding<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token function">generateHash<span class="token punctuation">(</span></span>body<span class="token punctuation">,</span> encoding<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// consider the function is defined
</span>  <span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre>

            </td>
      </tr>
    </tbody>
  </table>
</div>

  </section>

  <section>
    <h3 id="app.use">app.use([path,] function [, function...])</h3>

    <p>Mounts the <a href="http://www.expressjs.com.cn/guide/using-middleware.html">middleware</a> <code>function</code>(s) at the <code>path</code>. If <code>path</code> is not specified, it defaults to “/”.</p>

    <div class="doc-box doc-info">
      <p>A route will match any path, which follows its path immediately with a “<code>/</code>”.
  For example: <code>app.use('/apple', ...)</code> will match “/apple”, “/apple/images”,
  “/apple/images/news”, and so on.</p>
    </div>

    <div class="doc-box doc-info">
      <p><code>req.originalUrl</code> in a middleware is a combination of <code>req.baseUrl</code> and <code>req.path</code>, as shown in the following example.</p>

      <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/admin'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// GET 'http://www.example.com/admin/new'
</span>  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>req<span class="token punctuation">.</span>originalUrl<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// '/admin/new'
</span>  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>req<span class="token punctuation">.</span>baseUrl<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// '/admin'
</span>  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>req<span class="token punctuation">.</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// '/new'
</span>  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
    </div>

    <p>Mounting a middleware at a <code>path</code> will cause the middleware function to be executed whenever the base of the requested path matches the <code>path</code>.</p>

    <p>Since <code>path</code> defaults to “/”, middleware mounted without a path will be executed for every request to the app.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// this middleware will be executed for every request to the app
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'Time: %d'</span><span class="token punctuation">,</span> Date<span class="token punctuation">.</span><span class="token function">now<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

    <p>Middleware functions are executed sequentially, therefore the order of middleware inclusion is important.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// this middleware will not allow the request to go beyond it
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">// requests will never reach this route
</span>app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

    <p><code>path</code> can be a string representing a path, a path pattern, a regular expression to match paths,
or an array of combinations thereof.</p>

    <div class="doc-box doc-notice">
      <p>The middleware in the below are simple examples.</p>
    </div>

    <div class="table-scroller">
<table class="doctable" border="1">

  <thead>
    <tr>
      <th> Type </th>
      <th> Example </th>
    </tr>
  </thead>
  <tbody>

    <tr>
      <td>Path</td>
      <td>
        <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// will match paths starting with /abcd
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/abcd'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre>
      </td>
    </tr>

    <tr>
      <td>Path Pattern</td>
      <td>
        <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// will match paths starting with /abcd and /abd
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/abc?d'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">// will match paths starting with /abcd, /abbcd, /abbbbbcd and so on
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/ab+cd'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">// will match paths starting with /abcd, /abxcd, /abFOOcd, /abbArcd and so on
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/ab\*cd'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true">// will match paths starting with /ad and /abcd
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/a(bc)?d'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre>
      </td>
    </tr>

    <tr>
      <td>Regular Expression</td>
      <td>
        <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// will match paths starting with /abc and /xyz
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token regex">/\/abc|\/xyz/</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre>
      </td>
    </tr>

    <tr>
      <td>Array</td>
      <td>
        <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// will match paths starting with /abcd, /xyza, /lmn, and /pqr
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'/abcd'</span><span class="token punctuation">,</span> <span class="token string">'/xyza'</span><span class="token punctuation">,</span> <span class="token operator">/</span>\<span class="token operator">/</span>lmn<span class="token operator">|</span>\<span class="token operator">/</span>pqr<span class="token operator">/</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span></code></pre>
      </td>
    </tr>

  </tbody>

</table>
</div>

    <p><code>function</code> can be a middleware function, a series of middleware functions,
an array of middleware functions, or a combination of all of them.
Since <a href="http://www.expressjs.com.cn/4x/api.html#router" class="active">router</a> and <a href="http://www.expressjs.com.cn/4x/api.html#application">app</a> implement the middleware interface, you can use them
as you would any other middleware function.</p>

    <table class="doctable" border="1">

  <thead>
    <tr>
      <th>Usage</th>
      <th>Example</th>
    </tr>
  </thead>
  <tbody>

    <tr>
      <td>Single Middleware</td>
      <td>You can define and mount a middleware function locally.
<pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>
A router is valid middleware.

<pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> router <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
router<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>router<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

An Express app is valid middleware.
<pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> subApp <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
subApp<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>subApp<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
      </td>
    </tr>

    <tr>
      <td>Series of Middleware</td>
      <td>
        You can specify more than one middleware function at the same mount path.
<pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> r1 <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
r1<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token keyword">var</span> r2 <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
r2<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>r1<span class="token punctuation">,</span> r2<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
      </td>
    </tr>

    <tr>
      <td>Array</td>
      <td>
      Use an array to group middleware logically.
      If you pass an array of middleware as the first or only middleware parameters, then you _must_ specify the mount path.
<pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> r1 <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
r1<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token keyword">var</span> r2 <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
r2<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span>r1<span class="token punctuation">,</span> r2<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
      </td>
    </tr>

    <tr>
      <td>Combination</td>
      <td>
        You can combine all the above ways of mounting middleware.
<pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">function</span> <span class="token function">mw1<span class="token punctuation">(</span></span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">mw2<span class="token punctuation">(</span></span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>

<span class="token keyword">var</span> r1 <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
r1<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> r2 <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
r2<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> subApp <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
subApp<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>mw1<span class="token punctuation">,</span> <span class="token punctuation">[</span>mw2<span class="token punctuation">,</span> r1<span class="token punctuation">,</span> r2<span class="token punctuation">]</span><span class="token punctuation">,</span> subApp<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
      </td>
    </tr>

  </tbody>

</table>

    <p>Following are some examples of using the <a href="http://www.expressjs.com.cn/guide/using-middleware.html#middleware.built-in">express.static</a>
middleware in an Express app.</p>

    <p>Serve static content for the app from the “public” directory in the application directory:</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// GET /style.css etc
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>express<span class="token punctuation">.</span><span class="token keyword">static</span><span class="token punctuation">(</span>__dirname <span class="token operator">+</span> <span class="token string">'/public'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Mount the middleware at “/static” to serve static content only when their request path is prefixed with “/static”:</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// GET /static/style.css etc.
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/static'</span><span class="token punctuation">,</span> express<span class="token punctuation">.</span><span class="token keyword">static</span><span class="token punctuation">(</span>__dirname <span class="token operator">+</span> <span class="token string">'/public'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Disable logging for static content requests by loading the logger middleware after the static middleware:</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>express<span class="token punctuation">.</span><span class="token keyword">static</span><span class="token punctuation">(</span>__dirname <span class="token operator">+</span> <span class="token string">'/public'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token function">logger<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Serve static files from multiple directories, but give precedence to “./public” over the others:</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>express<span class="token punctuation">.</span><span class="token keyword">static</span><span class="token punctuation">(</span>__dirname <span class="token operator">+</span> <span class="token string">'/public'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>express<span class="token punctuation">.</span><span class="token keyword">static</span><span class="token punctuation">(</span>__dirname <span class="token operator">+</span> <span class="token string">'/files'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>express<span class="token punctuation">.</span><span class="token keyword">static</span><span class="token punctuation">(</span>__dirname <span class="token operator">+</span> <span class="token string">'/uploads'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <p><a id="req" class="h2"></a></p>
  <h2>Request</h2>

  <p>The <code>req</code> object represents the HTTP request and has properties for the 
request query string, parameters, body, HTTP headers, and so on.  In this documentation and by convention, 
the object is always referred to as <code>req</code> (and the HTTP response is <code>res</code>) but its actual name is determined
by the parameters to the callback function in which you’re working.</p>

  <p>For example:</p>

  <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'user '</span> <span class="token operator">+</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  <p>But you could just as well have:</p>

  <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">{</span>
  response<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'user '</span> <span class="token operator">+</span> request<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  <h3 id="req.properties">Properties</h3>

  <div class="doc-box doc-notice">
    <p>In Express 4, <code>req.files</code> is no longer available on the <code>req</code> object by default. To access uploaded files on the <code>req.files</code> object, use a multipart-handling middleware like <a href="https://www.npmjs.com/package/busboy">busboy</a>, <a href="https://www.npmjs.com/package/multer">multer</a>, <a href="https://www.npmjs.com/package/formidable">formidable</a>, <a href="https://www.npmjs.com/package/multiparty">multiparty</a>, <a href="https://www.npmjs.com/package/connect-multiparty">connect-multiparty</a>, or <a href="https://www.npmjs.com/package/pez">pez</a>.</p>
  </div>

  <section>
    <h3 id="req.app">req.app</h3>

    <p>This property holds a reference to the instance of the express application that is using the middleware.</p>

    <p>If you follow the pattern in which you create a module that just exports a middleware 
in order for requiring it in your main file, then the middleware can access the express instance via <code>req.app</code></p>

    <p>For example:</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">//index.js
</span>app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">"/viewdirectory"</span><span class="token punctuation">,</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">"./mymiddleware.js"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">//mymiddleware.js
</span>module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">"The views directory is "</span> <span class="token operator">+</span> req<span class="token punctuation">.</span>app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">"views"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="req.baseUrl">req.baseUrl</h3>

    <p>The URL path on which a router instance was mounted.
For example:</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> greet <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

greet<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/jp'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>req<span class="token punctuation">.</span>baseUrl<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// /greet
</span>  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Konichiwa!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/greet'</span><span class="token punctuation">,</span> greet<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// load the router on '/greet'
</span></code></pre>

    <p>Even if you use a path pattern or a set of path patterns to load the router,
the <code>baseUrl</code> property returns the matched string, not the pattern(s). In the
following example, the <code>greet</code> router is loaded on two path patterns.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'/gre+t'</span><span class="token punctuation">,</span> <span class="token string">'/hel{2}o'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> greet<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// load the router on '/gre+t' and '/hel{2}o'
</span></code></pre>

    <p>When a request is made to <code>/greet/jp</code>, <code>req.baseUrl</code> is “/greet”.  When a request is
made to <code>/hello/jp</code>, <code>req.baseUrl</code> is “/hello”.</p>

    <p><code>req.baseUrl</code> is similar to the <a href="http://www.expressjs.com.cn/4x/api.html#app.mountpath" class="active">mountpath</a> property of the <code>app</code> object,
except <code>app.mountpath</code> returns the matched path pattern(s).</p>

  </section>

  <section>
    <h3 id="req.body">req.body</h3>

    <p>Contains key-value pairs of data submitted in the request body.
By default, it is <code>undefined</code>, and is populated when you use body-parsing middleware such
as <a href="https://www.npmjs.org/package/body-parser">body-parser</a> and <a href="https://www.npmjs.org/package/multer">multer</a>.</p>

    <p>This example shows how to use body-parsing middleware to populate <code>req.body</code>.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> bodyParser <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'body-parser'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> multer <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'multer'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>bodyParser<span class="token punctuation">.</span><span class="token function">json<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// for parsing application/json
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>bodyParser<span class="token punctuation">.</span><span class="token function">urlencoded<span class="token punctuation">(</span></span><span class="token punctuation">{</span> extended<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// for parsing application/x-www-form-urlencoded
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token function">multer<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// for parsing multipart/form-data
</span>
app<span class="token punctuation">.</span><span class="token function">post<span class="token punctuation">(</span></span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>req<span class="token punctuation">.</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span>
  res<span class="token punctuation">.</span><span class="token function">json<span class="token punctuation">(</span></span>req<span class="token punctuation">.</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

  </section>

  <section>
    <h3 id="req.cookies">req.cookies</h3>

    <p>When using <a href="https://www.npmjs.com/package/cookie-parser">cookie-parser</a> middleware, this property is an object that
contains cookies sent by the request.  If the request contains no cookies, it defaults to <code>{}</code>.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// Cookie: name=tj
</span>req<span class="token punctuation">.</span>cookies<span class="token punctuation">.</span>name
<span class="token comment" spellcheck="true">// =&gt; "tj"
</span></code></pre>

    <p>For more information, issues, or concerns, see <a href="https://github.com/expressjs/cookie-parser">cookie-parser</a>.</p>

  </section>

  <section>
    <h3 id="req.fresh">req.fresh</h3>

    <p>Indicates whether the request is “fresh.”  It is the opposite of <code>req.stale</code>.</p>

    <p>It is true if the <code>cache-control</code> request header doesn’t have a <code>no-cache</code> directive and any
of the following is true:</p>

    <ul>
      <li>The <code>if-modified-since</code> request header is specified  and <code>last-modified</code> request header is equal to or eariler than the <code>modified</code> response header.</li>
      <li>The <code>if-none-match</code> request header is <code>*</code>.</li>
      <li>The <code>if-none-match</code> request header, after being parsed into its directives, does not
match the <code>etag</code> response header.</li>
    </ul>

    <pre class="  language-javascript"><code class="  language-javascript">req<span class="token punctuation">.</span>fresh
<span class="token comment" spellcheck="true">// =&gt; true
</span></code></pre>

    <p>For more information, issues, or concerns, see <a href="https://github.com/jshttp/fresh">fresh</a>.</p>

  </section>

  <section>
    <h3 id="req.hostname">req.hostname</h3>

    <p>Contains the hostname from the “Host” HTTP header.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// Host: "example.com:3000"
</span>req<span class="token punctuation">.</span>hostname
<span class="token comment" spellcheck="true">// =&gt; "example.com"
</span></code></pre>

  </section>

  <section>
    <h3 id="req.ip">req.ip</h3>

    <p>The remote IP address of the request.</p>

    <p>If the <code>trust proxy</code> is setting enabled, it is the upstream address;
see <a href="http://www.expressjs.com.cn/guide/behind-proxies.html">Express behind proxies</a> for more information.</p>

    <pre class="  language-javascript"><code class="  language-javascript">req<span class="token punctuation">.</span>ip
<span class="token comment" spellcheck="true">// =&gt; "127.0.0.1"
</span></code></pre>

  </section>

  <section>
    <h3 id="req.ips">req.ips</h3>

    <p>When the <code>trust proxy</code> setting is <code>true</code>, this property contains an array of
IP addresses specified in the “X-Forwarded-For” request header.  Otherwise, it contains an empty array.</p>

    <p>For example, if “X-Forwarded-For” is “client, proxy1, proxy2”, <code>req.ips</code> would be 
<code>["client", "proxy1", "proxy2"]</code>, where “proxy2” is the furthest downstream.</p>

    <p>For more information on the <code>trust proxy</code> setting, see <a href="http://www.expressjs.com.cn/4x/api.html#app.set" class="active">app.set</a>.</p>

  </section>

  <section>
    <h3 id="req.originalUrl">req.originalUrl</h3>

    <div class="doc-box doc-notice">
      <p><code>req.url</code> is not a native Express property, it is inherited from Node’s <a href="https://nodejs.org/api/http.html#http_message_url">http module</a>.</p>
    </div>

    <p>This property is much like <code>req.url</code>; however, it retains the original request URL,
allowing you to rewrite <code>req.url</code> freely for internal routing purposes. For example,
the “mounting” feature of <a href="http://www.expressjs.com.cn/4x/api.html#app.use" class="active">app.use()</a> will rewrite <code>req.url</code> to strip the mount point.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// GET /search?q=something
</span>req<span class="token punctuation">.</span>originalUrl
<span class="token comment" spellcheck="true">// =&gt; "/search?q=something"
</span></code></pre>

  </section>

  <section>
    <h3 id="req.params">req.params</h3>

    <p>An object containing properties mapped to the named route “parameters”. For example, if you have the route <code>/user/:name</code>, then the “name” property is available as <code>req.params.name</code>. This object defaults to <code>{}</code>.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// GET /user/tj
</span>req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>name
<span class="token comment" spellcheck="true">// =&gt; "tj"
</span></code></pre>

    <p>When you use a regular expression for the route definition, capture groups are provided in the array using <code>req.params[n]</code>, where <code>n</code> is the n<sup>th</sup> capture group. This rule is applied to unnamed wild card matches with string routes such as <code>/file/*</code>:</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// GET /file/javascripts/jquery.js
</span>req<span class="token punctuation">.</span>params<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
<span class="token comment" spellcheck="true">// =&gt; "javascripts/jquery.js"
</span></code></pre>

  </section>

  <section>
    <h3 id="req.path">req.path</h3>

    <p>Contains the path part of the request URL.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// example.com/users?sort=desc
</span>req<span class="token punctuation">.</span>path
<span class="token comment" spellcheck="true">// =&gt; "/users"
</span></code></pre>

    <div class="doc-box doc-info">
      <p>When called from a middleware, the mount point is not included in <code>req.path</code>. Look up <a href="http://www.expressjs.com.cn/4x/api.html#app.use">app.use()</a> for more details.</p>
    </div>

  </section>

  <section>
    <h3 id="req.protocol">req.protocol</h3>

    <p>The request protocol string, “http” or “https” when requested with TLS. When the “trust proxy” <a href="http://www.expressjs.com.cn/4x/api.html#trust.proxy.options.table">setting</a> trusts the socket address, the value of the “X-Forwarded-Proto” header (“http” or “https”) field will be trusted and used if present.</p>

    <pre class="  language-javascript"><code class="  language-javascript">req<span class="token punctuation">.</span>protocol
<span class="token comment" spellcheck="true">// =&gt; "http"
</span></code></pre>

  </section>

  <section>
    <h3 id="req.query">req.query</h3>

    <p>An object containing a property for each query string parameter in the route.
If there is no query string, it is the empty object, <code>{}</code>.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// GET /search?q=tobi+ferret
</span>req<span class="token punctuation">.</span>query<span class="token punctuation">.</span>q
<span class="token comment" spellcheck="true">// =&gt; "tobi ferret"
</span>
<span class="token comment" spellcheck="true">// GET /shoes?order=desc&amp;shoe[color]=blue&amp;shoe[type]=converse
</span>req<span class="token punctuation">.</span>query<span class="token punctuation">.</span>order
<span class="token comment" spellcheck="true">// =&gt; "desc"
</span>
req<span class="token punctuation">.</span>query<span class="token punctuation">.</span>shoe<span class="token punctuation">.</span>color
<span class="token comment" spellcheck="true">// =&gt; "blue"
</span>
req<span class="token punctuation">.</span>query<span class="token punctuation">.</span>shoe<span class="token punctuation">.</span>type
<span class="token comment" spellcheck="true">// =&gt; "converse"
</span></code></pre>

  </section>

  <section>
    <h3 id="req.route">req.route</h3>

    <p>The currently-matched route, a string.  For example:</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id?'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token function">userIdHandler<span class="token punctuation">(</span></span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>req<span class="token punctuation">.</span>route<span class="token punctuation">)</span><span class="token punctuation">;</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'GET'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

    <p>Example output from the previous snippet:</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token punctuation">{</span> path<span class="token punctuation">:</span> <span class="token string">'/user/:id?'</span><span class="token punctuation">,</span>
  stack<span class="token punctuation">:</span>
   <span class="token punctuation">[</span> <span class="token punctuation">{</span> handle<span class="token punctuation">:</span> <span class="token punctuation">[</span>Function<span class="token punctuation">:</span> userIdHandler<span class="token punctuation">]</span><span class="token punctuation">,</span>
       name<span class="token punctuation">:</span> <span class="token string">'userIdHandler'</span><span class="token punctuation">,</span>
       params<span class="token punctuation">:</span> undefined<span class="token punctuation">,</span>
       path<span class="token punctuation">:</span> undefined<span class="token punctuation">,</span>
       keys<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
       regexp<span class="token punctuation">:</span> <span class="token regex">/^\/?$/i</span><span class="token punctuation">,</span>
       method<span class="token punctuation">:</span> <span class="token string">'get'</span> <span class="token punctuation">}</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
  methods<span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token keyword">get</span><span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
</code></pre>

  </section>

  <section>
    <h3 id="req.secure">req.secure</h3>

    <p>A Boolean that is true if a TLS connection is established. Equivalent to:</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token string">'https'</span> <span class="token operator">==</span> req<span class="token punctuation">.</span>protocol<span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="req.signedCookies">req.signedCookies</h3>

    <p>When using <a href="https://www.npmjs.com/package/cookie-parser">cookie-parser</a> middleware, this property
contains signed cookies sent by the request, unsigned and ready for use. Signed cookies reside
in a different object to show developer intent; otherwise, a malicious attack could be placed on
<code>req.cookie</code> values (which are easy to spoof). Note that signing a cookie does not make it “hidden”
or encrypted; but simply prevents tampering (because the secret used to sign is private).
If no signed cookies are sent, the property defaults to <code>{}</code>.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// Cookie: user=tobi.CP7AWaXDfAKIRfH49dQzKJx7sKzzSoPq7/AcBBRVwlI3
</span>req<span class="token punctuation">.</span>signedCookies<span class="token punctuation">.</span>user
<span class="token comment" spellcheck="true">// =&gt; "tobi"
</span></code></pre>

    <p>For more information, issues, or concerns, see <a href="https://github.com/expressjs/cookie-parser">cookie-parser</a>.</p>

  </section>

  <section>
    <h3 id="req.stale">req.stale</h3>

    <p>Indicates whether the request is “stale,” and is the opposite of <code>req.fresh</code>.
For more information, see <a href="http://www.expressjs.com.cn/4x/api.html#req.fresh" class="active">req.fresh</a>.</p>

    <pre class="  language-javascript"><code class="  language-javascript">req<span class="token punctuation">.</span>stale
<span class="token comment" spellcheck="true">// =&gt; true
</span></code></pre>

  </section>

  <section>
    <h3 id="req.subdomains">req.subdomains</h3>

    <p>An array of subdomains in the domain name of the request.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// Host: "tobi.ferrets.example.com"
</span>req<span class="token punctuation">.</span>subdomains
<span class="token comment" spellcheck="true">// =&gt; ["ferrets", "tobi"]
</span></code></pre>

  </section>

  <section>
    <h3 id="req.xhr">req.xhr</h3>

    <p>A Boolean value that is <code>true</code> if the request’s “X-Requested-With” header field is
“XMLHttpRequest”, indicating that the request was issued by a client library such as jQuery.</p>

    <pre class="  language-javascript"><code class="  language-javascript">req<span class="token punctuation">.</span>xhr
<span class="token comment" spellcheck="true">// =&gt; true
</span></code></pre>

  </section>

  <h3 id="req.methods">Methods</h3>

  <section>
    <h3 id="req.accepts">req.accepts(types)</h3>

    <p>Checks if the specified content types are acceptable, based on the request’s <code>Accept</code> HTTP header field.
The method returns the best match, or if none of the specified content types is acceptable, returns 
<code>undefined</code> (in which case, the application should respond with <code>406 "Not Acceptable"</code>).</p>

    <p>The <code>type</code> value may be a single MIME type string (such as “application/json”),
an extension name such as “json”, a comma-delimited list, or an array. For a
list or array, the method returns the <em>best</em> match (if any).</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// Accept: text/html
</span>req<span class="token punctuation">.</span><span class="token function">accepts<span class="token punctuation">(</span></span><span class="token string">'html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; "html"
</span>
<span class="token comment" spellcheck="true">// Accept: text/*, application/json
</span>req<span class="token punctuation">.</span><span class="token function">accepts<span class="token punctuation">(</span></span><span class="token string">'html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; "html"
</span>req<span class="token punctuation">.</span><span class="token function">accepts<span class="token punctuation">(</span></span><span class="token string">'text/html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; "text/html"
</span>req<span class="token punctuation">.</span><span class="token function">accepts<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'json'</span><span class="token punctuation">,</span> <span class="token string">'text'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; "json"
</span>req<span class="token punctuation">.</span><span class="token function">accepts<span class="token punctuation">(</span></span><span class="token string">'application/json'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; "application/json"
</span>
<span class="token comment" spellcheck="true">// Accept: text/*, application/json
</span>req<span class="token punctuation">.</span><span class="token function">accepts<span class="token punctuation">(</span></span><span class="token string">'image/png'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
req<span class="token punctuation">.</span><span class="token function">accepts<span class="token punctuation">(</span></span><span class="token string">'png'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; undefined
</span>
<span class="token comment" spellcheck="true">// Accept: text/*;q=.5, application/json
</span>req<span class="token punctuation">.</span><span class="token function">accepts<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'html'</span><span class="token punctuation">,</span> <span class="token string">'json'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; "json"
</span></code></pre>

    <p>For more information, or if you have issues or concerns, see <a href="https://github.com/expressjs/accepts">accepts</a>.</p>

  </section>

  <section>
    <h3 id="req.acceptsCharsets">req.acceptsCharsets(charset [, ...])</h3>

    <p>Returns the first accepted charset of the specified character sets,
based on the request’s <code>Accept-Charset</code> HTTP header field.
If none of the specified charsets is accepted, returns <code>false</code>.</p>

    <p>For more information, or if you have issues or concerns, see <a href="https://github.com/expressjs/accepts">accepts</a>.</p>

  </section>

  <section>
    <h3 id="req.acceptsEncodings">req.acceptsEncodings(encoding [, ...])</h3>

    <p>Returns the first accepted encoding of the specified encodings,
based on the request’s <code>Accept-Encoding</code> HTTP header field.
If none of the specified encodings is accepted, returns <code>false</code>.</p>

    <p>For more information, or if you have issues or concerns, see <a href="https://github.com/expressjs/accepts">accepts</a>.</p>

  </section>

  <section>
    <h3 id="req.acceptsLanguages">req.acceptsLanguages(lang [, ...])</h3>

    <p>Returns the first accepted language of the specified languages,
based on the request’s <code>Accept-Language</code> HTTP header field.
If none of the specified languages is accepted, returns <code>false</code>.</p>

    <p>For more information, or if you have issues or concerns, see <a href="https://github.com/expressjs/accepts">accepts</a>.</p>

  </section>

  <section>
    <h3 id="req.get">req.get(field)</h3>

    <p>Returns the specified HTTP request header field (case-insensitive match).
The <code>Referrer</code> and <code>Referer</code> fields are interchangeable.</p>

    <pre class="  language-javascript"><code class="  language-javascript">req<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'Content-Type'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; "text/plain"
</span>
req<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'content-type'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; "text/plain"
</span>
req<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'Something'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; undefined
</span></code></pre>

    <p>Aliased as <code>req.header(field)</code>.</p>

  </section>

  <section>
    <h3 id="req.is">req.is(type)</h3>

    <p>Returns <code>true</code> if the incoming request’s “Content-Type” HTTP header field
matches the MIME type specified by the <code>type</code> parameter.
Returns <code>false</code> otherwise.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// With Content-Type: text/html; charset=utf-8
</span>req<span class="token punctuation">.</span><span class="token function">is<span class="token punctuation">(</span></span><span class="token string">'html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
req<span class="token punctuation">.</span><span class="token function">is<span class="token punctuation">(</span></span><span class="token string">'text/html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
req<span class="token punctuation">.</span><span class="token function">is<span class="token punctuation">(</span></span><span class="token string">'text/*'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; true
</span>
<span class="token comment" spellcheck="true">// When Content-Type is application/json
</span>req<span class="token punctuation">.</span><span class="token function">is<span class="token punctuation">(</span></span><span class="token string">'json'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
req<span class="token punctuation">.</span><span class="token function">is<span class="token punctuation">(</span></span><span class="token string">'application/json'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
req<span class="token punctuation">.</span><span class="token function">is<span class="token punctuation">(</span></span><span class="token string">'application/*'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; true
</span>
req<span class="token punctuation">.</span><span class="token function">is<span class="token punctuation">(</span></span><span class="token string">'html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; false
</span></code></pre>

    <p>For more information, or if you have issues or concerns, see <a href="https://github.com/expressjs/type-is">type-is</a>.</p>

  </section>

  <section>
    <h3 id="req.param">req.param(name [, defaultValue])</h3>

    <div class="doc-box doc-warn">
      <p>Deprecated. Use either <code>req.params</code>, <code>req.body</code> or <code>req.query</code>, as applicable.</p>
    </div>

    <p>Return the value of param <code>name</code> when present.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// ?name=tobi
</span>req<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// =&gt; "tobi"
</span>
<span class="token comment" spellcheck="true">// POST name=tobi
</span>req<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// =&gt; "tobi"
</span>
<span class="token comment" spellcheck="true">// /user/tobi for /user/:name 
</span>req<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// =&gt; "tobi"
</span></code></pre>

    <p>Lookup is performed in the following order:</p>

    <ul>
      <li><code>req.params</code></li>
      <li><code>req.body</code></li>
      <li><code>req.query</code></li>
    </ul>

    <p>Optionally, you can specify <code>defaultValue</code> to set a default value if the parameter is not found in any of the request objects.</p>

    <div class="doc-box doc-warn">
      <p>Direct access to <code>req.body</code>, <code>req.params</code>, and <code>req.query</code> should be favoured for clarity - unless you truly accept input from each object.</p>

      <p>Body-parsing middleware must be loaded for <code>req.param()</code> to work predictably. Refer <a href="http://www.expressjs.com.cn/4x/api.html#req.body" class="active">req.body</a> for details.</p>
    </div>

  </section>

  <p><a id="res" class="h2"></a></p>
  <h2>Response</h2>

  <p>The <code>res</code> object represents the HTTP response that an Express app sends when it gets an HTTP request.</p>

  <p>In this documentation and by convention, 
the object is always referred to as <code>res</code> (and the HTTP request is <code>req</code>) but its actual name is determined
by the parameters to the callback function in which you’re working.</p>

  <p>For example:</p>

  <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'user '</span> <span class="token operator">+</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  <p>But you could just as well have:</p>

  <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">{</span>
  response<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'user '</span> <span class="token operator">+</span> request<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  <h3 id="res.properties">Properties</h3>

  <section>
    <h3 id="res.app">res.app</h3>

    <p>This property holds a reference to the instance of the express application that is using the middleware.</p>

    <p><code>res.app</code> is identical to the <a href="http://www.expressjs.com.cn/4x/api.html#req.app" class="active">req.app</a> property in the request object.</p>

  </section>

  <section>
    <h3 id="res.headersSent">res.headersSent</h3>

    <p>Boolean property that indicates if the app sent HTTP headers for the response.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>res<span class="token punctuation">.</span>headersSent<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// false
</span>  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'OK'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>res<span class="token punctuation">.</span>headersSent<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// true
</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

  </section>

  <section>
    <h3 id="res.locals">res.locals</h3>

    <p>An object that contains response local variables scoped to the request, and therefore available only to
the view(s) rendered during that request / response cycle (if any). Otherwise,
this property is identical to <a href="http://www.expressjs.com.cn/4x/api.html#app.locals" class="active">app.locals</a>.</p>

    <p>This property is useful for exposing request-level information such as the request path name,
authenticated user, user settings, and so on.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span><span class="token punctuation">{</span>
  res<span class="token punctuation">.</span>locals<span class="token punctuation">.</span>user <span class="token operator">=</span> req<span class="token punctuation">.</span>user<span class="token punctuation">;</span>
  res<span class="token punctuation">.</span>locals<span class="token punctuation">.</span>authenticated <span class="token operator">=</span> <span class="token operator">!</span> req<span class="token punctuation">.</span>user<span class="token punctuation">.</span>anonymous<span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <h3 id="res.methods">Methods</h3>

  <section>
    <h3 id="res.append">res.append(field [, value])</h3>

    <div class="doc-box doc-info">
      <p><code>res.append()</code> is supported by Express v4.11.0+</p>
    </div>

    <p>Appends the specified <code>value</code> to the HTTP response header <code>field</code>.  If the header is not already set,
it creates the header with the specified value.  The <code>value</code> parameter can be a string or an array.</p>

    <p>Note: calling <code>res.set()</code> after <code>res.append()</code> will reset the previously-set header value.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">append<span class="token punctuation">(</span></span><span class="token string">'Link'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'&lt;http://localhost/&gt;'</span><span class="token punctuation">,</span> <span class="token string">'&lt;http://localhost:3000/&gt;'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">append<span class="token punctuation">(</span></span><span class="token string">'Set-Cookie'</span><span class="token punctuation">,</span> <span class="token string">'foo=bar; Path=/; HttpOnly'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">append<span class="token punctuation">(</span></span><span class="token string">'Warning'</span><span class="token punctuation">,</span> <span class="token string">'199 Miscellaneous warning'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="res.attachment">res.attachment([filename])</h3>

    <p>Sets the HTTP response <code>Content-Disposition</code> header field to “attachment”. If a <code>filename</code> is given,
then it sets the Content-Type based on the extension name via <code>res.type()</code>,
and sets the <code>Content-Disposition</code> “filename=” parameter.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">attachment<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// Content-Disposition: attachment
</span>
res<span class="token punctuation">.</span><span class="token function">attachment<span class="token punctuation">(</span></span><span class="token string">'path/to/logo.png'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// Content-Disposition: attachment; filename="logo.png"
</span><span class="token comment" spellcheck="true">// Content-Type: image/png
</span></code></pre>

  </section>

  <section>
    <h3 id="res.cookie">res.cookie(name, value [, options])</h3>

    <p>Sets cookie <code>name</code> to <code>value</code>.  The <code>value</code> parameter may be a string or object converted to JSON.</p>

    <p>The <code>options</code> parameter is an object that can have the following properties.</p>

    <table>
      <thead>
        <tr>
          <th>Property</th>
          <th>Type</th>
          <th>Description</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><code>domain</code></td>
          <td>String</td>
          <td>Domain name for the cookie. Defaults to the domain name of the app.</td>
        </tr>
        <tr>
          <td><code>expires</code></td>
          <td>Date</td>
          <td>Expiry date of the cookie in GMT. If not specified or set to 0, creates a session cookie.</td>
        </tr>
        <tr>
          <td><code>httpOnly</code></td>
          <td>Boolean</td>
          <td>Flags the cookie to be accessible only by the web server.</td>
        </tr>
        <tr>
          <td><code>maxAge</code></td>
          <td>String</td>
          <td>Convenient option for setting the expiry time relative to the current time in milliseconds.</td>
        </tr>
        <tr>
          <td><code>path</code></td>
          <td>String</td>
          <td>Path for the cookie. Defaults to “/”.</td>
        </tr>
        <tr>
          <td><code>secure</code></td>
          <td>Boolean</td>
          <td>Marks the cookie to be used with HTTPS only.</td>
        </tr>
        <tr>
          <td><code>signed</code></td>
          <td>Boolean</td>
          <td>Indicates if the cookie should be signed.</td>
        </tr>
      </tbody>
    </table>

    <div class="doc-box doc-notice">
      <p>All <code>res.cookie()</code> does is set the HTTP <code>Set-Cookie</code> header with the options provided.
Any option not specified defaults to the value stated in <a href="http://tools.ietf.org/html/rfc6265">RFC 6265</a>.</p>
    </div>

    <p>For example:</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">cookie<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'tobi'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> domain<span class="token punctuation">:</span> <span class="token string">'.example.com'</span><span class="token punctuation">,</span> path<span class="token punctuation">:</span> <span class="token string">'/admin'</span><span class="token punctuation">,</span> secure<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">cookie<span class="token punctuation">(</span></span><span class="token string">'rememberme'</span><span class="token punctuation">,</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> expires<span class="token punctuation">:</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>Date<span class="token punctuation">.</span><span class="token function">now<span class="token punctuation">(</span></span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">900000</span><span class="token punctuation">)</span><span class="token punctuation">,</span> httpOnly<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>The <code>maxAge</code> option is a convenience option for setting “expires” relative to the current time in milliseconds.
The following is equivalent to the second example above.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">cookie<span class="token punctuation">(</span></span><span class="token string">'rememberme'</span><span class="token punctuation">,</span> <span class="token string">'1'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> maxAge<span class="token punctuation">:</span> <span class="token number">900000</span><span class="token punctuation">,</span> httpOnly<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

    <p>You can pass an object as the <code>value</code> parameter; it is then serialized as JSON and parsed by <code>bodyParser()</code> middleware.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">cookie<span class="token punctuation">(</span></span><span class="token string">'cart'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> items<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">cookie<span class="token punctuation">(</span></span><span class="token string">'cart'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> items<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> maxAge<span class="token punctuation">:</span> <span class="token number">900000</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>When using <a href="https://www.npmjs.com/package/cookie-parser">cookie-parser</a> middleware, this method also
supports signed cookies. Simply include the <code>signed</code> option set to <code>true</code>.
Then <code>res.cookie()</code> will use the secret passed to <code>cookieParser(secret)</code> to sign the value.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">cookie<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'tobi'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> signed<span class="token punctuation">:</span> <span class="token keyword">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Later you may access this value through the <a href="http://www.expressjs.com.cn/4x/api.html#req.signedCookies" class="active">req.signedCookie</a> object.</p>

  </section>

  <section>
    <h3 id="res.clearCookie">res.clearCookie(name [, options])</h3>

    <p>Clears the cookie specified by <code>name</code>. For details about the <code>options</code> object, see <a href="http://www.expressjs.com.cn/4x/api.html#res.cookie" class="active">res.cookie()</a>.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">cookie<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'tobi'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> path<span class="token punctuation">:</span> <span class="token string">'/admin'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">clearCookie<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> path<span class="token punctuation">:</span> <span class="token string">'/admin'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="res.download">res.download(path [, filename] [, fn])</h3>

    <p>Transfers the file at <code>path</code> as an “attachment”. Typically, browsers will prompt the user for download.
By default, the <code>Content-Disposition</code> header “filename=” parameter is <code>path</code> (this typically appears in the brower dialog).
Override this default with the <code>filename</code> parameter.</p>

    <p>When an error ocurrs or transfer is complete, the method calls the optional callback function <code>fn</code>.
This method uses <a href="http://www.expressjs.com.cn/4x/api.html#res.sendFile" class="active">res.sendFile()</a> to transfer the file.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">download<span class="token punctuation">(</span></span><span class="token string">'/report-12345.pdf'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

res<span class="token punctuation">.</span><span class="token function">download<span class="token punctuation">(</span></span><span class="token string">'/report-12345.pdf'</span><span class="token punctuation">,</span> <span class="token string">'report.pdf'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

res<span class="token punctuation">.</span><span class="token function">download<span class="token punctuation">(</span></span><span class="token string">'/report-12345.pdf'</span><span class="token punctuation">,</span> <span class="token string">'report.pdf'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// Handle error, but keep in mind the response may be partially-sent
</span>    <span class="token comment" spellcheck="true">// so check res.headersSent
</span>  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// decrement a download credit, etc.
</span>  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="res.end">res.end([data] [, encoding])</h3>

    <p>Ends the response process. This method actually comes from Node core, specifically the <a href="https://nodejs.org/api/http.html#http_response_end_data_encoding_callback">response.end() method of  http.ServerResponse</a>.</p>

    <p>Use to quickly end the response without any data. If you need to respond with data, instead use methods such as <a href="http://www.expressjs.com.cn/4x/api.html#res.send" class="active">res.send()</a> and <a href="http://www.expressjs.com.cn/4x/api.html#res.json" class="active">res.json()</a>.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">end<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span><span class="token number">404</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">end<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="res.format">res.format(object)</h3>

    <p>Performs content-negotiation on the <code>Accept</code> HTTP header on the request object, when present.
It uses <a href="http://www.expressjs.com.cn/4x/api.html#req.accepts" class="active">req.accepts()</a> to select a handler for the request, based on the acceptable
types ordered by their quality values. If the header is not specified, the first callback is invoked.
When no match is found, the server responds with 406 “Not Acceptable”, or invokes the <code>default</code> callback.</p>

    <p>The <code>Content-Type</code> response header is set when a callback is selected. However, you may alter
this within the callback using methods such as <code>res.set()</code> or <code>res.type()</code>.</p>

    <p>The following example would respond with <code>{ "message": "hey" }</code> when the <code>Accept</code> header field is set
to “application/json” or “*/json” (however if it is “*/*”, then the response will be “hey”).</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">format<span class="token punctuation">(</span></span><span class="token punctuation">{</span>
  <span class="token string">'text/plain'</span><span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'hey'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>

  <span class="token string">'text/html'</span><span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'&lt;p&gt;hey&lt;/p&gt;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>

  <span class="token string">'application/json'</span><span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token punctuation">{</span> message<span class="token punctuation">:</span> <span class="token string">'hey'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>

  <span class="token string">'default'</span><span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// log the request and respond with 406
</span>    res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span><span class="token number">406</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Not Acceptable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>In addition to canonicalized MIME types, you may also use extension names mapped
to these types for a slightly less verbose implementation:</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">format<span class="token punctuation">(</span></span><span class="token punctuation">{</span>
  text<span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'hey'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>

  html<span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'&lt;p&gt;hey&lt;/p&gt;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>

  json<span class="token punctuation">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token punctuation">{</span> message<span class="token punctuation">:</span> <span class="token string">'hey'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="res.get">res.get(field)</h3>

    <p>Returns the HTTP response header specified by <code>field</code>.
The match is case-insensitive.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'Content-Type'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">// =&gt; "text/plain"
</span></code></pre>

  </section>

  <section>
    <h3 id="res.json">res.json([body])</h3>

    <p>Sends a JSON response. This method is identical to <code>res.send()</code> with an object or array as the parameter.
However, you can use it to convert other values to JSON, such as <code>null</code>, and <code>undefined</code>.
(although these are technically not valid JSON).</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">json<span class="token punctuation">(</span></span><span class="token keyword">null</span><span class="token punctuation">)</span>
res<span class="token punctuation">.</span><span class="token function">json<span class="token punctuation">(</span></span><span class="token punctuation">{</span> user<span class="token punctuation">:</span> <span class="token string">'tobi'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">json<span class="token punctuation">(</span></span><span class="token punctuation">{</span> error<span class="token punctuation">:</span> <span class="token string">'message'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

  </section>

  <section>
    <h3 id="res.jsonp">res.jsonp([body])</h3>

    <p>Sends a JSON response with JSONP support. This method is identical to <code>res.json()</code>,
except that it opts-in to JSONP callback support.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">jsonp<span class="token punctuation">(</span></span><span class="token keyword">null</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// =&gt; null
</span>
res<span class="token punctuation">.</span><span class="token function">jsonp<span class="token punctuation">(</span></span><span class="token punctuation">{</span> user<span class="token punctuation">:</span> <span class="token string">'tobi'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// =&gt; { "user": "tobi" }
</span>
res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">jsonp<span class="token punctuation">(</span></span><span class="token punctuation">{</span> error<span class="token punctuation">:</span> <span class="token string">'message'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// =&gt; { "error": "message" }
</span></code></pre>

    <p>By default, the JSONP callback name is simply <code>callback</code>. Override this with the
<a href="http://www.expressjs.com.cn/4x/api.html#app.settings.table">jsonp callback name</a> setting.</p>

    <p>The following are some examples of JSONP responses using the same code:</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// ?callback=foo
</span>res<span class="token punctuation">.</span><span class="token function">jsonp<span class="token punctuation">(</span></span><span class="token punctuation">{</span> user<span class="token punctuation">:</span> <span class="token string">'tobi'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// =&gt; foo({ "user": "tobi" })
</span>
app<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'jsonp callback name'</span><span class="token punctuation">,</span> <span class="token string">'cb'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// ?cb=foo
</span>res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">jsonp<span class="token punctuation">(</span></span><span class="token punctuation">{</span> error<span class="token punctuation">:</span> <span class="token string">'message'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true">// =&gt; foo({ "error": "message" })
</span></code></pre>

  </section>

  <section>
    <h3 id="res.links">res.links(links)</h3>

    <p>Joins the <code>links</code> provided as properties of the parameter to populate the response’s
<code>Link</code> HTTP header field.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">links<span class="token punctuation">(</span></span><span class="token punctuation">{</span>
  next<span class="token punctuation">:</span> <span class="token string">'http://api.example.com/users?page=2'</span><span class="token punctuation">,</span>
  last<span class="token punctuation">:</span> <span class="token string">'http://api.example.com/users?page=5'</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>yields:</p>

    <pre class="  language-javascript"><code class="  language-javascript">Link<span class="token punctuation">:</span> <span class="token operator">&lt;</span>http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>api<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token operator">/</span>users<span class="token operator">?</span>page<span class="token operator">=</span><span class="token number">2</span><span class="token operator">&gt;</span><span class="token punctuation">;</span> rel<span class="token operator">=</span><span class="token string">"next"</span><span class="token punctuation">,</span> 
      <span class="token operator">&lt;</span>http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>api<span class="token punctuation">.</span>example<span class="token punctuation">.</span>com<span class="token operator">/</span>users<span class="token operator">?</span>page<span class="token operator">=</span><span class="token number">5</span><span class="token operator">&gt;</span><span class="token punctuation">;</span> rel<span class="token operator">=</span><span class="token string">"last"</span>
</code></pre>

  </section>

  <section>
    <h3 id="res.location">res.location(path)</h3>

    <p>Sets the response <code>Location</code> HTTP header to the specified <code>path</code> parameter.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">location<span class="token punctuation">(</span></span><span class="token string">'/foo/bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">location<span class="token punctuation">(</span></span><span class="token string">'http://example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">location<span class="token punctuation">(</span></span><span class="token string">'back'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>A <code>path</code> value of “back” has a special meaning, it refers to the URL specified in the <code>Referer</code> header of the request. If the <code>Referer</code> header was not specified, it refers to “/”.</p>

    <div class="doc-box doc-warn">
      <p>Express passes the specified URL string as-is to the browser in the <code>Location</code> header,
without any validation or manipulation, except in case of <code>back</code>.</p>

      <p>Browsers take the responsibility of deriving the intended URL from the current URL
or the referring URL, and the URL specified in the <code>Location</code> header; and redirect the user accordingly.</p>
    </div>

  </section>

  <section>
    <h3 id="res.redirect">res.redirect([status,] path)</h3>

    <p>Redirects to the URL dervied from the specified <code>path</code>, with specified
<a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">HTTP status code</a> <code>status</code>.
If you don’t specify <code>status</code>, the status code defaults to “302 “Found”.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'/foo/bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'http://example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">redirect<span class="token punctuation">(</span></span><span class="token number">301</span><span class="token punctuation">,</span> <span class="token string">'http://example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'../login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
    <p>Redirects can be a fully-qualified URL for redirecting to a different site:</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'http://google.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
    <p>Redirects can be relative to the root of the host name. For example, if the
application is on <code>http://example.com/admin/post/new</code>, the following
would redirect to the URL <code>http://example.com/admin</code>:</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'/admin'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Redirects can be relative to the current URL. For example, 
from <code>http://example.com/blog/admin/</code> (notice the trailing slash), the following
would redirect to the URL <code>http://example.com/blog/admin/post/new</code>.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'post/new'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Redirecting to <code>post/new</code> from <code>http://example.com/blog/admin</code> (no trailing slash),
will redirect to <code>http://example.com/blog/post/new</code>.</p>

    <p>If you found the above behavior confusing, think of path segments as directories
(with trailing slashes) and files, it will start to make sense.</p>

    <p>Path-relative redirects are also possible. If you were on
<code>http://example.com/admin/post/new</code>, the following would redirect to
<code>http//example.com/admin/post</code>:</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'..'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>A <code>back</code> redirection redirects the request back to the <a href="http://en.wikipedia.org/wiki/HTTP_referer">referer</a>,
defaulting to <code>/</code> when the referer is missing.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">redirect<span class="token punctuation">(</span></span><span class="token string">'back'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    
</code></pre>

  </section>

  <section>
    <h3 id="res.render">res.render(view [, locals] [, callback])</h3>

    <p>Renders a <code>view</code> and sends the rendered HTML string to the client.
Optional parameters:</p>

    <ul>
      <li><code>locals</code>, an object whose properties define local variables for the view.</li>
      <li><code>callback</code>, a callback function. If provided, the method returns both the possible error and rendered string, but does not perform an automated response. When an error occurs, the method invokes <code>next(err)</code> internally.</li>
    </ul>

    <div class="doc-box doc-notice">
      <p>The local variable <code>cache</code> enables view caching. Set it to <code>true</code>,
to cache the view during development; view caching is enabled in production by default.</p>
    </div>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// send the rendered view to the client
</span>res<span class="token punctuation">.</span><span class="token function">render<span class="token punctuation">(</span></span><span class="token string">'index'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// if a callback is specified, the rendered HTML string has to be sent explicitly
</span>res<span class="token punctuation">.</span><span class="token function">render<span class="token punctuation">(</span></span><span class="token string">'index'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> html<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span>html<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// pass a local variable to the view
</span>res<span class="token punctuation">.</span><span class="token function">render<span class="token punctuation">(</span></span><span class="token string">'user'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> name<span class="token punctuation">:</span> <span class="token string">'Tobi'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> html<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// ...
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="res.send">res.send([body])</h3>

    <p>Sends the HTTP response.</p>

    <p>The <code>body</code> parameter can be a <code>Buffer</code> object, a <code>String</code>, an object, or an <code>Array</code>.
For example:</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">Buffer</span><span class="token punctuation">(</span><span class="token string">'whoop'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token punctuation">{</span> some<span class="token punctuation">:</span> <span class="token string">'json'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'&lt;p&gt;some html&lt;/p&gt;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span><span class="token number">404</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Sorry, we cannot find that!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token punctuation">{</span> error<span class="token punctuation">:</span> <span class="token string">'something blew up'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>This method performs many useful tasks for simple non-streaming responses:
For example, it automatically assigns the <code>Content-Length</code> HTTP response header field
(unless previously defined) and provides automatic HEAD and HTTP cache freshness support.</p>

    <p>When the parameter is a <code>Buffer</code> object, the method sets the <code>Content-Type</code>
response header field  to “application/octet-stream”, unless previously defined as shown below:</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'Content-Type'</span><span class="token punctuation">,</span> <span class="token string">'text/html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">Buffer</span><span class="token punctuation">(</span><span class="token string">'&lt;p&gt;some html&lt;/p&gt;'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>When the parameter is a <code>String</code>, the method sets the <code>Content-Type</code> to “text/html”:</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'&lt;p&gt;some html&lt;/p&gt;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>When the parameter is an <code>Array</code> or <code>Object</code>, Express responds with the JSON representation:</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token punctuation">{</span> user<span class="token punctuation">:</span> <span class="token string">'tobi'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="res.sendFile">res.sendFile(path [, options] [, fn])</h3>

    <div class="doc-box doc-info">
      <p><code>res.sendFile()</code> is supported from Express v4.8.0 onwards</p>
    </div>

    <p>Transfers the file at the given <code>path</code>. Sets the <code>Content-Type</code> response HTTP header field
based on the filename’s extension. Unless the <code>root</code> option is set in
the options object, <code>path</code> must be an absolute path of the file.</p>

    <p>The details of the <code>options</code> object is listed in the following table.</p>

    <div class="table-scroller">

      <table>
        <thead>
          <tr>
            <th>Property</th>
            <th>Description</th>
            <th>Default</th>
            <th>Availability</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td><code>maxAge</code></td>
            <td>Sets the max-age property of the <code>Cache-Control</code> header in milliseconds or a string in <a href="https://www.npmjs.org/package/ms">ms format</a></td>
            <td>0</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><code>root</code></td>
            <td>Root directory for relative filenames.</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><code>lastModified</code></td>
            <td>Sets the <code>Last-Modified</code> header to the last modified date of the file on the OS. Set <code>false</code> to disable it.</td>
            <td>Enabled</td>
            <td>4.9.0+</td>
          </tr>
          <tr>
            <td><code>headers</code></td>
            <td>Object containing HTTP headers to serve with the file.</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><code>dotfiles</code></td>
            <td>Option for serving dotfiles. Possible values are “allow”, “deny”, “ignore”.</td>
            <td>“ignore”</td>
            <td>&nbsp;</td>
          </tr>
        </tbody>
      </table>

    </div>

    <p>The method invokes the callback function <code>fn(err)</code> when the transfer is complete
or when an error occurs. If the callback function is specified and an error occurs,
the callback function must explicitly handle the response process either by
ending the request-response cycle, or by passing control to the next route.</p>

    <p>Here is an example of using <code>res.sendFile</code> with all its arguments.</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/file/:name'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token keyword">var</span> options <span class="token operator">=</span> <span class="token punctuation">{</span>
    root<span class="token punctuation">:</span> __dirname <span class="token operator">+</span> <span class="token string">'/public/'</span><span class="token punctuation">,</span>
    dotfiles<span class="token punctuation">:</span> <span class="token string">'deny'</span><span class="token punctuation">,</span>
    headers<span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token string">'x-timestamp'</span><span class="token punctuation">:</span> Date<span class="token punctuation">.</span><span class="token function">now<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token string">'x-sent'</span><span class="token punctuation">:</span> <span class="token keyword">true</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>

  <span class="token keyword">var</span> fileName <span class="token operator">=</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
  res<span class="token punctuation">.</span><span class="token function">sendFile<span class="token punctuation">(</span></span>fileName<span class="token punctuation">,</span> options<span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>
      res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span>err<span class="token punctuation">.</span>status<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">end<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>
      console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'Sent:'</span><span class="token punctuation">,</span> fileName<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

    <p><code>res.sendFile</code> provides fine-grained support for file serving as illustrated in the following example:</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:uid/photos/:file'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token keyword">var</span> uid <span class="token operator">=</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>uid
    <span class="token punctuation">,</span> file <span class="token operator">=</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>file<span class="token punctuation">;</span>

  req<span class="token punctuation">.</span>user<span class="token punctuation">.</span><span class="token function">mayViewFilesFrom<span class="token punctuation">(</span></span>uid<span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>yes<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>yes<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      res<span class="token punctuation">.</span><span class="token function">sendFile<span class="token punctuation">(</span></span><span class="token string">'/uploads/'</span> <span class="token operator">+</span> uid <span class="token operator">+</span> <span class="token string">'/'</span> <span class="token operator">+</span> file<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span><span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Sorry! you cant see that.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
    <p>For more information, or if you have issues or concerns, see <a href="https://github.com/pillarjs/send">send</a>.</p>

  </section>

  <section>
    <h3 id="res.sendStatus">res.sendStatus(statusCode)</h3>

    <p>Set the response HTTP status code to <code>statusCode</code> and send its string representation as the response body.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">sendStatus<span class="token punctuation">(</span></span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// equivalent to res.status(200).send('OK')
</span>res<span class="token punctuation">.</span><span class="token function">sendStatus<span class="token punctuation">(</span></span><span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// equivalent to res.status(403).send('Forbidden')
</span>res<span class="token punctuation">.</span><span class="token function">sendStatus<span class="token punctuation">(</span></span><span class="token number">404</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// equivalent to res.status(404).send('Not Found')
</span>res<span class="token punctuation">.</span><span class="token function">sendStatus<span class="token punctuation">(</span></span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// equivalent to res.status(500).send('Internal Server Error')
</span></code></pre>

    <p>If an unsupported status code is specified, the HTTP status is still set to <code>statusCode</code> and the string version of the code is sent as the response body.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">sendStatus<span class="token punctuation">(</span></span><span class="token number">2000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// equivalent to res.status(2000).send('2000')
</span></code></pre>

    <p><a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes">More about HTTP Status Codes</a></p>

  </section>

  <section>
    <h3 id="res.set">res.set(field [, value])</h3>

    <p>Sets the response’s HTTP header <code>field</code> to <code>value</code>.
To set multiple fields at once, pass an object as the parameter.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token string">'Content-Type'</span><span class="token punctuation">,</span> <span class="token string">'text/plain'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

res<span class="token punctuation">.</span><span class="token keyword">set</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token string">'Content-Type'</span><span class="token punctuation">:</span> <span class="token string">'text/plain'</span><span class="token punctuation">,</span>
  <span class="token string">'Content-Length'</span><span class="token punctuation">:</span> <span class="token string">'123'</span><span class="token punctuation">,</span>
  <span class="token string">'ETag'</span><span class="token punctuation">:</span> <span class="token string">'12345'</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

    <p>Aliased as <code>res.header(field [, value])</code>.</p>

  </section>

  <section>
    <h3 id="res.status">res.status(code)</h3>

    <p>Use this method to set the HTTP status for the response.
It is a chainable alias of Node’s <a href="http://nodejs.org/api/http.html#http_response_statuscode">response.statusCode</a>.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span><span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">end<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span><span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Bad Request'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token punctuation">.</span><span class="token function">status<span class="token punctuation">(</span></span><span class="token number">404</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">sendFile<span class="token punctuation">(</span></span><span class="token string">'/absolute/path/to/404.png'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="res.type">res.type(type)</h3>

    <p>Sets the <code>Content-Type</code> HTTP header to the MIME type as determined by
<a href="https://github.com/broofa/node-mime#mimelookuppath">mime.lookup()</a> for the specified <code>type</code>.
If <code>type</code> contains the “/” character, then it sets the <code>Content-Type</code> to <code>type</code>.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">type<span class="token punctuation">(</span></span><span class="token string">'.html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>              <span class="token comment" spellcheck="true">// =&gt; 'text/html'
</span>res<span class="token punctuation">.</span><span class="token function">type<span class="token punctuation">(</span></span><span class="token string">'html'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>               <span class="token comment" spellcheck="true">// =&gt; 'text/html'
</span>res<span class="token punctuation">.</span><span class="token function">type<span class="token punctuation">(</span></span><span class="token string">'json'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>               <span class="token comment" spellcheck="true">// =&gt; 'application/json'
</span>res<span class="token punctuation">.</span><span class="token function">type<span class="token punctuation">(</span></span><span class="token string">'application/json'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment" spellcheck="true">// =&gt; 'application/json'
</span>res<span class="token punctuation">.</span><span class="token function">type<span class="token punctuation">(</span></span><span class="token string">'png'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                <span class="token comment" spellcheck="true">// =&gt; image/png:
</span></code></pre>

  </section>

  <section>
    <h3 id="res.vary">res.vary(field)</h3>

    <p>Adds the field to the <code>Vary</code> response header, if it is not there already.</p>

    <pre class="  language-javascript"><code class="  language-javascript">res<span class="token punctuation">.</span><span class="token function">vary<span class="token punctuation">(</span></span><span class="token string">'User-Agent'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">render<span class="token punctuation">(</span></span><span class="token string">'docs'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <p><a id="router" class="h2"></a></p>
  <h2>Router</h2>

  <section>
    <p>A <code>router</code> object is an isolated instance of middleware and routes. You can think of it
as a “mini-application,” capable only of performing middleware and routing
functions. Every Express application has a built-in app router.</p>

    <p>A router behaves like middleware itself, so you can use it as an argument to
<a href="http://www.expressjs.com.cn/4x/api.html#app.use" class="active">app.use()</a> or as the argument to another router’s  <a href="http://www.expressjs.com.cn/4x/api.html#router.use" class="active">use()</a> method.</p>

    <p>The top-level <code>express</code> object has a <code>Router()</code> function that creates a new <code>router</code> object.</p>

    <h3 id="router">Router([options])</h3>

    <p>Create a new router as follows:</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> router <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">[</span>options<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>The optional <code>options</code> parameter specifies the behavior of the router.</p>

    <div class="table-scroller">

      <table>
        <thead>
          <tr>
            <th>Property</th>
            <th>Description</th>
            <th>Default</th>
            <th>Availability</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td><code>caseSensitive</code></td>
            <td>Enable case sensitivity.</td>
            <td>Disabled by default, treating “/Foo” and “/foo” as the same.</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td><code>mergeParams</code></td>
            <td>Preserve the <code>req.params</code> values from the parent router. If the parent and the child have conflicting param names, the child’s value take precedence.</td>
            <td><code>false</code></td>
            <td>4.5.0+</td>
          </tr>
          <tr>
            <td><code>strict</code></td>
            <td>Enable strict routing.</td>
            <td>Disabled by default, “/foo” and “/foo/” are treated the same by the router.</td>
            <td>&nbsp;</td>
          </tr>
        </tbody>
      </table>

    </div>

    <p>You can add middleware and HTTP method routes (such as <code>get</code>, <code>put</code>, <code>post</code>, and
so on) to <code>router</code> just like an application.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// invoked for any requests passed to this router
</span>router<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// .. some logic here .. like any other middleware
</span>  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// will handle any request that ends in /events
</span><span class="token comment" spellcheck="true">// depends on where the router is "use()'d"
</span>router<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/events'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// ..
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>You can then use a router for a particular root URL in this way separating your routes into files or even mini-apps.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token comment" spellcheck="true">// only requests to /calendar/* will be sent to our "router"
</span>app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/calendar'</span><span class="token punctuation">,</span> router<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <h3 id="router.methods">Methods</h3>

  <section>
    <h3 id="router.all">router.all(path, [callback, ...] callback)</h3>

    <p>This method functions just like the <code>router.METHOD()</code> methods, except that it matches all HTTP verbs.</p>

    <p>This method is extremely useful for
mapping “global” logic for specific path prefixes or arbitrary matches.
For example, if you placed the following route at the top of all other
route definitions, it would require that all routes from that point on
would require authentication, and automatically load a user. Keep in mind
that these callbacks do not have to act as end points; <code>loadUser</code>
can perform a task, then call <code>next()</code> to continue matching subsequent
routes.</p>

    <pre class="  language-javascript"><code class="  language-javascript">router<span class="token punctuation">.</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token string">'*'</span><span class="token punctuation">,</span> requireAuthentication<span class="token punctuation">,</span> loadUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Or the equivalent:</p>

    <pre class="  language-javascript"><code class="  language-javascript">router<span class="token punctuation">.</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token string">'*'</span><span class="token punctuation">,</span> requireAuthentication<span class="token punctuation">)</span>
router<span class="token punctuation">.</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token string">'*'</span><span class="token punctuation">,</span> loadUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Another example of this is white-listed “global” functionality. Here
the example is much like before, but it only restricts paths prefixed with
“/api”:</p>

    <pre class="  language-javascript"><code class="  language-javascript">router<span class="token punctuation">.</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token string">'/api/*'</span><span class="token punctuation">,</span> requireAuthentication<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="router.METHOD">router.METHOD(path, [callback, ...] callback)</h3>

    <p>The <code>router.METHOD()</code> methods provide the routing functionality in Express,
where METHOD is one of the HTTP methods, such as GET, PUT, POST, and so on,
in lowercase.  Thus, the actual methods are <code>router.get()</code>, <code>router.post()</code>,
<code>router.put()</code>, and so on.</p>

    <p>You can provide multiple callbacks, and all are treated equally, and behave just
like middleware, except that these callbacks may invoke <code>next('route')</code>
to bypass the remaining route callback(s).  You can use this mechanism to perform
pre-conditions on a route then pass control to subsequent routes when there is no
reason to proceed with the route matched.</p>

    <p>The following snippet illustrates the most simple route definition possible.
Express translates the path strings to regular expressions, used internally
to match incoming requests. Query strings are <em>not</em> considered when performing
these matches, for example “GET /” would match the following route, as would
“GET /?name=tobi”.</p>

    <pre class="  language-javascript"><code class="  language-javascript">router<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'hello world'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>You can also use regular expressions—useful if you have very specific
constraints, for example the following would match “GET /commits/71dbb9c” as well
as “GET /commits/71dbb9c..4c084f9”.</p>

    <pre class="  language-javascript"><code class="  language-javascript">router<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token regex">/^\/commits\/(\w+)(?:\.\.(\w+))?$/</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token keyword">var</span> from <span class="token operator">=</span> req<span class="token punctuation">.</span>params<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> to <span class="token operator">=</span> req<span class="token punctuation">.</span>params<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">||</span> <span class="token string">'HEAD'</span><span class="token punctuation">;</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'commit range '</span> <span class="token operator">+</span> from <span class="token operator">+</span> <span class="token string">'..'</span> <span class="token operator">+</span> to<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="router.param">router.param([name,] callback)</h3>

    <p>Add callback triggers to route parameters, where <code>name</code> is the name of the parameter or an array of them, and <code>function</code> is the callback function. The parameters of the callback function are the request object, the response object, the next middleware, and the value of the parameter, in that order.</p>

    <p>If <code>name</code> is an array, the <code>callback</code> trigger is registered for each parameter declared in it, in the order in which they are declared. Furthermore, for each declared parameter except the last one, a call to <code>next</code> inside the callback will call the callback for the next declared parameter. For the last parameter, a call to <code>next</code> will call the next middleware in place for the route currently being processed, just like it would if <code>name</code> were just a string.</p>

    <p>For example, when <code>:user</code> is present in a route path, you may map user loading logic to automatically provide <code>req.user</code> to the route, or perform validations on the parameter input.</p>

    <pre class="  language-javascript"><code class="  language-javascript">router<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'user'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">,</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token comment" spellcheck="true">// try to get the user details from the User model and attach it to the request object
</span>  User<span class="token punctuation">.</span><span class="token function">find<span class="token punctuation">(</span></span>id<span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>err<span class="token punctuation">,</span> user<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">next<span class="token punctuation">(</span></span>err<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>user<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      req<span class="token punctuation">.</span>user <span class="token operator">=</span> user<span class="token punctuation">;</span>
      <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token function">next<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">'failed to load user'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Param callback functions are local to the router on which they are defined. They are not inherited by mounted apps or routers. Hence, param callbacks defined on <code>router</code> will be triggered only by route parameters defined on <code>router</code> routes.</p>

    <p>A param callback will be called only once in a request-response cycle, even if the parameter is matched in multiple routes, as shown in the following examples.</p>

    <pre class="  language-javascript"><code class="  language-javascript">router<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">,</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'CALLED ONLY ONCE'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'although this matches'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'and this matches too'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  res<span class="token punctuation">.</span><span class="token function">end<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>On <code>GET /user/42</code>, the following is printed:</p>

    <pre><code>CALLED ONLY ONCE
although this matches
and this matches too
</code></pre>

    <pre class="  language-javascript"><code class="  language-javascript">router<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'id'</span><span class="token punctuation">,</span> <span class="token string">'page'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">,</span> value<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'CALLED ONLY ONCE with'</span><span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id/:page'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'although this matches'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id/:page'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'and this matches too'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  res<span class="token punctuation">.</span><span class="token function">end<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>On <code>GET /user/42/3</code>, the following is printed:</p>

    <pre><code>CALLED ONLY ONCE with 42
CALLED ONLY ONCE with 3
although this matches
and this matches too
</code></pre>

    <div class="doc-box doc-warn">
      <p>The following section describes <code>router.param(callback)</code>, which is deprecated as of v4.11.0.</p>
    </div>

    <p>The behavior of the <code>router.param(name, callback)</code> method can be altered entirely by passing only a function to <code>router.param()</code>. This function is a custom implementation of how <code>router.param(name, callback)</code> should behave - it accepts two parameters and must return a middleware.</p>

    <p>The first parameter of this function is the name of the URL parameter that should be captured, the second parameter can be any JavaScript object which might be used for returning the middleware implementation.</p>

    <p>The middleware returned by the function decides the behavior of what happens when a URL parameter is captured.</p>

    <p>In this example, the <code>router.param(name, callback)</code> signature is modified to <code>router.param(name, accessId)</code>. Instead of accepting a name and a callback, <code>router.param()</code> will now accept a name and a number.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> express <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> router <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// customizing the behavior of router.param()
</span>router<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>param<span class="token punctuation">,</span> option<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>val <span class="token operator">==</span> option<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>
      res<span class="token punctuation">.</span><span class="token function">sendStatus<span class="token punctuation">(</span></span><span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// using the customized router.param()
</span>router<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">,</span> <span class="token number">1337</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// route to trigger the capture
</span>router<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token string">'/user/:id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'OK'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>router<span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">listen<span class="token punctuation">(</span></span><span class="token number">3000</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'Ready'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

    <p>In this example, the <code>router.param(name, callback)</code> signature remains the same, but instead of a middleware callback, a custom data type checking function has been defined to validate the data type of the user id.</p>

    <pre class="  language-javascript"><code class="  language-javascript">router<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>param<span class="token punctuation">,</span> validator<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">validator<span class="token punctuation">(</span></span>val<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>
      res<span class="token punctuation">.</span><span class="token function">sendStatus<span class="token punctuation">(</span></span><span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

router<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span>candidate<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token operator">!</span><span class="token function">isNaN<span class="token punctuation">(</span></span><span class="token function">parseFloat<span class="token punctuation">(</span></span>candidate<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">isFinite<span class="token punctuation">(</span></span>candidate<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

  </section>

  <section>
    <h3 id="router.route">router.route(path)</h3>

    <p>Returns an instance of a single route which you can then use to handle HTTP verbs
with optional middleware. Use <code>router.route()</code> to avoid duplicate route naming and
thus typo errors.</p>

    <p>Building on the <code>router.param()</code> example above, the following code shows how to use
<code>router.route()</code> to specify various HTTP method handlers.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> router <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

router<span class="token punctuation">.</span><span class="token function">param<span class="token punctuation">(</span></span><span class="token string">'user_id'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">,</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// sample user, would actually fetch from DB, etc...
</span>  req<span class="token punctuation">.</span>user <span class="token operator">=</span> <span class="token punctuation">{</span>
    id<span class="token punctuation">:</span> id<span class="token punctuation">,</span>
    name<span class="token punctuation">:</span> <span class="token string">'TJ'</span>
  <span class="token punctuation">}</span><span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

router<span class="token punctuation">.</span><span class="token function">route<span class="token punctuation">(</span></span><span class="token string">'/users/:user_id'</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">all<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// runs for all HTTP verbs first
</span>  <span class="token comment" spellcheck="true">// think of it as route specific middleware!
</span>  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token keyword">get</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">json<span class="token punctuation">(</span></span>req<span class="token punctuation">.</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">put<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// just an example of maybe updating the user
</span>  req<span class="token punctuation">.</span>user<span class="token punctuation">.</span>name <span class="token operator">=</span> req<span class="token punctuation">.</span>params<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
  <span class="token comment" spellcheck="true">// save user ... etc
</span>  res<span class="token punctuation">.</span><span class="token function">json<span class="token punctuation">(</span></span>req<span class="token punctuation">.</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">post<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">'not implemented'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token keyword">delete</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">'not implemented'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre>

    <p>This approach re-uses the single ‘/users/:user_id’ path and add handlers for
various HTTP methods.</p>

  </section>

  <section>
    <h3 id="router.use">router.use([path], [function, ...] function)</h3>

    <p>Uses the given middleware <code>function</code>, with optional mount path <code>path</code>, that defaults to “/”.</p>

    <p>This method is similar to <a href="http://www.expressjs.com.cn/4x/api.html#app.use" class="active">app.use()</a>. A simple example and usecase is described below.
See <a href="http://www.expressjs.com.cn/4x/api.html#app.use" class="active">app.use()</a> for more information.</p>

    <p>Middleware is like a plumbing pipe, requests start at the first middleware you define
and work their way “down” the middleware stack processing for each path they match.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> express <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'express'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> app <span class="token operator">=</span> <span class="token function">express<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> router <span class="token operator">=</span> express<span class="token punctuation">.</span><span class="token function">Router<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// simple logger for this router's requests
</span><span class="token comment" spellcheck="true">// all requests to this router will first hit this middleware
</span>router<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log<span class="token punctuation">(</span></span><span class="token string">'%s %s %s'</span><span class="token punctuation">,</span> req<span class="token punctuation">.</span>method<span class="token punctuation">,</span> req<span class="token punctuation">.</span>url<span class="token punctuation">,</span> req<span class="token punctuation">.</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// this will only be invoked if the path starts with /bar from the mount point
</span>router<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/bar'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// ... maybe some additional /bar logging ...
</span>  <span class="token function">next<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// always invoked
</span>router<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">,</span> next<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token string">'/foo'</span><span class="token punctuation">,</span> router<span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">listen<span class="token punctuation">(</span></span><span class="token number">3000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>The “mount” path is stripped and is <em>not</em> visible to the middleware <code>function</code>.
The main effect of this feature is that mounted middleware may operate without
code changes regardless of its “prefix” pathname.</p>

    <p>The order in which you define middleware with <code>router.use()</code> is very important.
They are invoked sequentially, thus the order defines middleware precedence. For example,
usually a logger is the very first middleware you would use, so every request is logged.</p>

    <pre class="  language-javascript"><code class="  language-javascript"><span class="token keyword">var</span> logger <span class="token operator">=</span> <span class="token function">require<span class="token punctuation">(</span></span><span class="token string">'morgan'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

router<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token function">logger<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
router<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>express<span class="token punctuation">.</span><span class="token keyword">static</span><span class="token punctuation">(</span>__dirname <span class="token operator">+</span> <span class="token string">'/public'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
router<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Hello'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Now suppose you wanted to ignore logging requests for static files, but to continue
logging routes and middleware defined after <code>logger()</code>.  You would simply move <code>static()</code> above:</p>

    <pre class="  language-javascript"><code class="  language-javascript">router<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>express<span class="token punctuation">.</span><span class="token keyword">static</span><span class="token punctuation">(</span>__dirname <span class="token operator">+</span> <span class="token string">'/public'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
router<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token function">logger<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
router<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span><span class="token keyword">function</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send<span class="token punctuation">(</span></span><span class="token string">'Hello'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>Another concrete example is serving files from multiple directories,
giving precedence to “./public” over the others:</p>

    <pre class="  language-javascript"><code class="  language-javascript">app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>express<span class="token punctuation">.</span><span class="token keyword">static</span><span class="token punctuation">(</span>__dirname <span class="token operator">+</span> <span class="token string">'/public'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>express<span class="token punctuation">.</span><span class="token keyword">static</span><span class="token punctuation">(</span>__dirname <span class="token operator">+</span> <span class="token string">'/files'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">use<span class="token punctuation">(</span></span>express<span class="token punctuation">.</span><span class="token keyword">static</span><span class="token punctuation">(</span>__dirname <span class="token operator">+</span> <span class="token string">'/uploads'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>

    <p>The <code>router.use()</code> method also supports named parameters so that your mount points
for other routers can benefit from preloading using named parameters.</p>

  </section>

</div>


    </section>

    <a id="top" href="http://www.expressjs.com.cn/4x/api.html#"><img src="./Express 4.x - API 中文手册_files/arrow.png"></a>
    
    <footer>
        <div id="footer-content">
            <div id="github">
                <a href="https://github.com/strongloop/express" target="_blank">Github Repo</a>
            </div>
            <div id="sponsor">Express 项目由 <a href="http://strongloop.com/" target="_blank">StrongLoop</a> 赞助。</div>
            <div id="fork"><a href="https://github.com/strongloop/expressjs.com/tree/gh-pages/_includes/api/en/4x">Edit this page on GitHub</a></div>
            <div>Copyright © StrongLoop, Inc., and other expressjs.com contributors.</div>
        </div>
    
        <div class="friend-links">
            <a href="http://www.bootcss.com/" title="Bootstrap 中文网">Bootstrap中文网</a>
            <a href="http://www.gruntjs.net/" title="Grunt 中文网">Grunt中文网</a>
            <a href="http://www.gulpjs.com.cn/" title="Gulp 中文网">Gulp中文网</a>
            <a href="http://www.bootcdn.cn/" title="BootCDN 加速">BootCDN</a>
            <a href="http://www.nodeapp.cn/" title="Node.js 中文文档">Node.js 中文文档</a>
        </div>
    </footer>
    
    <div class="beian">
    	<a href="http://www.miibeian.gov.cn/" target="_blank">冀ICP备15008021号</a><span>|</span>© Express 中文网 2015
    </div>
  


</body></html>